Random numbers

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
beanz
Microbot
Posts: 183
Joined: Wed Mar 21, 2018 1:10 pm
Location: Texas USA

Random numbers

Post by beanz »

Years ago I wrote a flat horse racing game in basic that had used random numbers to help generate the stats for the horses. I'm "thinking" about attempting rewriting this as the original is long lost.

Now one probably I remember having back then and never addressed it was I noticed after a while that the "alleged" random numbers always started out the same..which then affected the rest of the game so you could basically cheat as you knew what the random number was going to be...even powered off and loaded from tape it would still randomly generate the same number on first run. At the time I never figured out why...I assumed the rnd command wasn't so random after all or the program was "remembering/storing" the base number that was used to generate the random number.

A few years after that I remember reading someone posting a similar problem they were having and someone responded with "you need to put a CLEAR XXXXX command at the start of your program" so it's really a random number.

Anyone know what that CLEAR command would be or how else to ensure REAL randomness for each game?.
User avatar
arkannoyed
Manic Miner
Posts: 435
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Random numbers

Post by arkannoyed »

Why not use a modern z80 equivalent? There’s even a XOR shift n ( or something like that!) that’s only 30 bytes-ish?!
User avatar
arkannoyed
Manic Miner
Posts: 435
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Random numbers

Post by arkannoyed »

User avatar
Morkin
Bugaboo
Posts: 3251
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: Random numbers

Post by Morkin »

I actually like the idea of organised randomness... :D

You can use a "PRESS ANY KEY" method. On an intro screen after the game has loaded for example.

Cycle through a "random number of random numbers" at the start of the game while waiting for a key input. Something like:

Code: Select all

10 RANDOMIZE
20 PRINT "PRESS ANY KEY"
25 LET A=RND
30 IF INKEY$="" THEN GO TO 25
40 PRINT RND
Try saving the above as a snapshot, run it, reload it and run it again. You should get different sequences.

If you do the same without line 25 you'll probably get similar sequences coming up each time you load the snapshot.

Not as good as the impressive XOR shift method that [mention]arkannoyed[/mention] mentioned of course.
My Speccy site: thirdharmoniser.com
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: Random numbers

Post by Einar Saukas »

beanz wrote: Wed May 15, 2019 5:41 pmAnyone know what that CLEAR command would be or how else to ensure REAL randomness for each game?.
It's RANDOMIZE, not CLEAR.

Running RANDOMIZE without parameters will use current value of FRAMES (i.e how many frames since the machine was turned on) as random seed.

In practice, wait for some kind of player input before executing RANDOMIZE. It will do the trick. For instance:

Code: Select all

10 PRINT "Press S to start"
20 IF INKEY$<>"s" THEN GO TO 20
30 RANDOMIZE
40 PRINT RND
50 GO TO 40
User avatar
beanz
Microbot
Posts: 183
Joined: Wed Mar 21, 2018 1:10 pm
Location: Texas USA

Re: Random numbers

Post by beanz »

Einar Saukas wrote: Wed May 15, 2019 6:28 pm
beanz wrote: Wed May 15, 2019 5:41 pmAnyone know what that CLEAR command would be or how else to ensure REAL randomness for each game?.
It's RANDOMIZE, not CLEAR.

Running RANDOMIZE without parameters will use current value of FRAMES (i.e how many frames since the machine was turned on) as random seed.

In practice, wait for some kind of player input before executing RANDOMIZE. It will do the trick. For instance:

Code: Select all

10 PRINT "Press S to start"
20 IF INKEY$<>"s" THEN GO TO 20
30 RANDOMIZE
40 PRINT RND
50 GO TO 40
Ah that's it!, thanks.
User avatar
1bvl109
Dizzy
Posts: 98
Joined: Tue Jun 04, 2019 9:00 pm

Re: Random numbers

Post by 1bvl109 »

Fortunately you don't need any fancy kind of randomness, only unforeseeability. This is a 8-bit version of arkanoyed's proposal http://www.z80.info/pseudo-random.txt and this is the theory with some code in Phyton and Pascal which should be transferable to BASIC.
Besides 1) the frames counter and 2) waiting for a key being pressed you can use 3) the R-register as a seed, or as part of it, because there you only get 7 bits. This however is not available through BASIC.
Then of course there is 4) a table with "random" numbers built right into the ZX82, i.e, the ROM. However you still need a number their you want to start PEEKing from that table.
So this

Code: Select all

ld hl,23673 ;FRAMES counter middle byte
ld a,hl
ld l,a
ld a,r 
and $1f
ld h,a
ld a,r
add (hl)
ld hl,23672 ;FRAMES counter least significant byte
xor (hl)
may work too. I didn't get around to actually testing it though.
"Truth would quickly cease to be stranger than fiction, once we got used to it." - H.L. Mencken
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Random numbers

Post by djnzx48 »

The highest quality Z80 RNG available is probably Patrik Rak's one (which may be the same one that arkannoyed posted). The one in that link looks like a linear congruential generator, and those are notoriously poor in terms of quality.

There's a page here with some interesting comparisons of different RNGs.

The R register and FRAMES are useful for seeding an RNG, but they're not so good if you use them every call as your only source of randomness. For games, you often don't need a very high quality generator anyway. SaNchez games for example use a simple LCRNG combined with the R register, but it works well ingame and you don't notice any problems with randomness.
Post Reply