Random numbers

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

Random numbers

Post by beanz » Wed May 15, 2019 4:41 pm

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?.
1 x

User avatar
arkannoyed
Manic Miner
Posts: 310
Joined: Mon Feb 05, 2018 9:56 am

Re: Random numbers

Post by arkannoyed » Wed May 15, 2019 4:43 pm

Why not use a modern z80 equivalent? There’s even a XOR shift n ( or something like that!) that’s only 30 bytes-ish?!
0 x

User avatar
arkannoyed
Manic Miner
Posts: 310
Joined: Mon Feb 05, 2018 9:56 am

Re: Random numbers

Post by arkannoyed » Wed May 15, 2019 4:48 pm

0 x

User avatar
Morkin
Manic Miner
Posts: 466
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: Random numbers

Post by Morkin » Wed May 15, 2019 4:57 pm

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 @arkannoyed mentioned of course.
2 x

User avatar
Einar Saukas
Manic Miner
Posts: 692
Joined: Wed Nov 15, 2017 2:48 pm

Re: Random numbers

Post by Einar Saukas » Wed May 15, 2019 5:28 pm

beanz wrote:
Wed May 15, 2019 4:41 pm
Anyone 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
2 x

User avatar
beanz
Microbot
Posts: 109
Joined: Wed Mar 21, 2018 1:10 pm
Location: Texas USA

Re: Random numbers

Post by beanz » Wed May 15, 2019 5:37 pm

Einar Saukas wrote:
Wed May 15, 2019 5:28 pm
beanz wrote:
Wed May 15, 2019 4:41 pm
Anyone 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.
0 x

Post Reply