 # A closer lookatrandom numbers

May 13, 2003 12:07 AM

Remember that all numbers produced by electronic devices are only pseudo-random: While they exhibit all the qualities of random numbers, they are not truly random.

This week, I’ll show you a method of producing a series of pseudo-random numbers. This method is very simple. You start off with an arbitrary seed number. You take its square root, accurate to six digits. You then extract the fourth and fifth decimal digits to use as a random number. You then use this number as the new seed.

Using 13 as the initial seed number, we get results like these:

SEED= 13 SQR OF SEED= 3.605551 RANDOM NUMBER= 55

SEED= 55 SQR OF SEED= 7.416199 RANDOM NUMBER= 19

SEED= 19 SQR OF SEED= 4.358899 RANDOM NUMBER= 89

SEED= 89 SQR OF SEED= 9.433981 RANDOM NUMBER= 98

SEED= 98 SQR OF SEED= 9.899495 RANDOM NUMBER= 49

SEED= 49 SQR OF SEED= 7.000000 RANDOM NUMBER= 0

As you can clearly see, the series of numbers appears to follow a random pattern. There is only one problem. After producing only five numbers, our system starts producing nothing but zeroes! We must refine our system so that if a zero is produced, the seed is modified. Let’s say that if the random number produced is zero, we’ll increment the seed by one and continue.

SEED= 13 SQR OF SEED= 3.605551 RANDOM NUMBER= 55

SEED= 55 SQR OF SEED= 7.416199 RANDOM NUMBER= 19

SEED= 19 SQR OF SEED= 4.358899 RANDOM NUMBER= 89

SEED= 89 SQR OF SEED= 9.433981 RANDOM NUMBER= 98

SEED= 98 SQR OF SEED= 9.899495 RANDOM NUMBER= 49

SEED= 50 SQR OF SEED= 7.071068 RANDOM NUMBER= 6

SEED= 6 SQR OF SEED= 2.44949 RANDOM NUMBER= 48

SEED= 48 SQR OF SEED= 6.928203 RANDOM NUMBER= 20

SEED= 20 SQR OF SEED= 4.472136 RANDOM NUMBER= 13

SEED= 13 SQR OF SEED= 3.605551 RANDOM NUMBER= 55

SEED= 55 SQR OF SEED= 7.416199 RANDOM NUMBER= 19

SEED= 19 SQR OF SEED= 4.358899 RANDOM NUMBER= 89

SEED= 89 SQR OF SEED= 9.433981 RANDOM NUMBER= 98

SEED= 98 SQR OF SEED= 9.899495 RANDOM NUMBER= 49

SEED= 50 SQR OF SEED= 7.071068 RANDOM NUMBER= 6

SEED= 6 SQR OF SEED= 2.44949 RANDOM NUMBER= 48

Of course now we’ve just traded one problem for another. Our random number sequence no longer gets stuck on zero, but it does cycle over and over again with the same values. More sophisticated routines have been developed to lengthen these cycles to many thousands of numbers, but all such routines are ultimately cyclical. They all repeat sooner or later.

To avoid this situation, computer companies vary the initial seed every time the routine is used. This is usually done by using the clock that is built into the computer. A digital signal of the time and date is used for a seed each time the routine is run. Thus a series of pseudo random numbers is created, but if you know the seed number and the routine, you will of course know which numbers will be produced. That is why these numbers are only pseudo-random.

Below, you will find a BASIC program that will produce pseudo random numbers using the routine discussed above. I have modified it a little in step 120 to lengthen its cyclic period. Have fun!

10 INPUT SEED

20 TE=SQR(SEED)

30 LPRINT "SEED=";SEED;" SQR OF SEED=";TE;

40 TE=TE*1000

50 TE=TE-INT(TE)

60 TE=TE*100

70 TE=INT(TE)

80 IF TE= 0 THEN 90 ELSE 110

90 SEED=SEED+1

100 GOTO 20

110 LPRINT "RANDOM NUMBER=",TE

120 SEED=(TE^(1/3))^13

130 GOTO 20

Well, that’s it for this week, good luck, I’ll see you in line!