by H. L. Schaaf
A few months ago Duane Parker and I had been talking about random numbers. I told him about a program that I had done which gives the seed of any random number, that is to say it works in reverse of the random number generator. Others may also be interested, so here it is.. While in 2068 format, it’s easy to modify for the ZX-81 and QL.
BASIC Listing1234
10 REM "seed<-->RND"
20 LET a=2^16
25 LET b=a+1
30 LET c=52
40 LET d=74
50 LET e=d+1
60 LET f=.5
70 PRINT "You enter the value you want to come up as the next random # ."
80 PRINT '"If you enter a number between Zero and Unity ( 0 to 1 ) Then it will be converted to an integer multiple of 65535."
90 PRINT '"You may enter an integer betweenZero and 65535 and the decimal value (0 to 1) will be shown"''
100 INPUT "Random number between 0 & 65535 that you want to know seed of ";r
102 IF r<0 THEN PRINT "POSITIVE VALUES ONLY !"''"Press ENTER to restart": PAUSE 0: RUN
103 IF r>1 AND (r-INT r)<>0 THEN PRINT "INTEGERS ONLY !"''"Press ENTER to restart": PAUSE 0: RUN
105 IF r>1 THEN LET r=INT (f+r*a)
107 IF r=74 THEN FLASH 1: PRINT "That's a tough one ! The seed of ZERO will NOT be predictable !!": PRINT '" RANDOMIZE 74 gives the longest nonrepeating run of random numbers.": FLASH 0: PRINT
110 PRINT "For RND = ";r;" ( ";r/a;" )"
120 LET x=r-d
125 LET x=INT ((x-((INT (x/e))*e))+f)
130 LET x=x*c
140 LET x=INT ((x-((INT (x/e))*e))+f)
150 LET seed=INT (((x*b+r-d)/e)+f)
160 PRINT "RANDOMIZE ";seed;" and then RND"
200 REM rnd from seed
205 PRINT '"Given a seed this will find the Resulting RND the hard way."
210 INPUT "seed ? ";seed
220 PRINT seed,
230 LET random = (seed*75+74)
250 IF random<b THEN GO TO 260
255 LET random=random-b: GO TO 250
260 PRINT random
270 PRINT '"Now the easy way !"
280 RANDOMIZE seed
290 PRINT seed, RND*a
295 PRINT '"Press ENTER to RUN again"
300 PAUSE 0: RUN
I’ve also done a couple of FORTH screens that will do this seed<–>rnd conversion and generation if anyone cares.
Now what I’d like to see is an explanation of just how the QL does its random numbers!
Products
Media
Image Gallery
Source Code
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.