How does RND effect RAND?

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
MrVertigo
Posts: 112
Joined: Fri May 27, 2022 9:06 pm

How does RND effect RAND?

Post by MrVertigo »

If I peek the RAND variable after a refresh, it contains the number zero. If I use RND once, the RAND variable now contains 74. If I use RND again the RAND variable now contains 5624, then 28652 etc. This makes me realise I don’t really understand how RND works. I thought it stepped through a pseudo random sequence in sequential order. Can anyone explain it to me?
User avatar
1024MAK
Posts: 5526
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: How does RND effect RAND?

Post by 1024MAK »

Do you mean 16434 (0x4032) "SEED", the Random Number Seed system variable?

Have a read of chapter 5 in the ZX81 BASIC programming book.

The system variable is updated after each use of RND, but as you have seen, it's not in a sequential sequence. Rather, it's a new seed value derived from RND ready for the next RND.

If you multiply the result of RND by 65536, it should be the same as SEED.

Code: Select all

1 POKE 16434,0
2 POKE 16435,0
10 PRINT "BEFORE: ";PEEK 16434+256*PEEK 16435
20 LET R=RND
30 PRINT R;" ";R*65536
40 PRINT "AFTER: ";PEEK 16434+256*PEEK 16435
50 PRINT
60 INPUT A$
70 IF A$="" THEN GOTO 10
RND
RND
RND.png (5 KiB) Viewed 2646 times

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Spring approaching...
MrVertigo
Posts: 112
Joined: Fri May 27, 2022 9:06 pm

Re: How does RND effect RAND?

Post by MrVertigo »

Ah, yes- so the RND numbers ARE in sequential order, but the SEEDS (RAND system variable (16434, 16435) are made random?

If I do this, it gives the actual RND sequential sequence:

10 Let X=1
20 RAND X
30 PRINT RND
40 LET X=X+1
50 GOTO 20
Post Reply