Added a second PRNG with longer period

This commit is contained in:
Andrew Alderwick 2021-08-25 21:59:30 +01:00
parent f4e9e2e915
commit 5b4ec0be6b
1 changed files with 31 additions and 12 deletions

View File

@ -1,7 +1,6 @@
( pseudo-random number generator ) ( pseudo-random number generator,
based on two 16-bit xorshift algorithms by George Marsaglia
( based on 16-bit xorshift algorithm discussed on http://www.jstatsoft.org/v08/i14/paper )
http://www.retroprogramming.com/2017/07/xorshift-pseudorandom-numbers-in-z80.html )
( devices ) ( devices )
@ -18,17 +17,22 @@
|0100 ( -> ) |0100 ( -> )
( init ) ( init )
;on-frame .Screen/vector DEO2 ;on-frame .Screen/vector DEO2
( seed prng (must be nonzero) ) ( seed prng (must be nonzero) )
.DateTime/month DEI .DateTime/day DEI #00 .DateTime/second DEI
#00 .DateTime/second #a0 SFT2 EOR2 #00 .DateTime/minute DEI #60 SFT2 EOR2
#00 .DateTime/minute #40 SFT2 EOR2 #00 .DateTime/hour DEI #c0 SFT2 EOR2 ;prng2/x STA2
.DateTime/hour EOR #00 .DateTime/hour DEI #04 SFT2
#00 .DateTime/day DEI #10 SFT2 EOR2
#00 .DateTime/month DEI #60 SFT2 EOR2
#00 .DateTime/year DEI #a0 SFT2 EOR2 ;prng2/y STA2
;prng2/x LDA2 ;prng2/y LDA2 EOR2
ORAk ,&non-zero JCN INC2 &non-zero ORAk ,&non-zero JCN INC2 &non-zero
;prng/seed STA2 ;prng/seed STA2
( theme ) ( theme )
#0fe5 .System/r DEO2 #0fe5 .System/r DEO2
#0fc5 .System/g DEO2 #0fc5 .System/g DEO2
#0f25 .System/b DEO2 #0f25 .System/b DEO2
BRK BRK
@ -42,16 +46,17 @@
BRK BRK
@draw-pixel @draw-pixel
,prng JSR ,prng2 JSR
#00 SWP .Screen/x DEO2 #00 SWP .Screen/x DEO2
#00 SWP .Screen/y DEO2 #00 SWP .Screen/y DEO2
#01 .Screen/pixel DEO #01 .Screen/pixel DEO
JMP2r JMP2r
@prng ( -- number* ) @prng ( -- number* )
( returns the next number in the 65,535-long sequence, ( returns the next number in a 65,535-long sequence,
which is never zero but every other 16-bit number which is never zero but every other 16-bit number
appears once before the sequence repeats ) appears once before the sequence repeats )
( http://www.retroprogramming.com/2017/07/ xorshift-pseudorandom-numbers-in-z80.html )
,&seed LDR2 ,&seed LDR2
DUP2 #70 SFT2 EOR2 DUP2 #70 SFT2 EOR2
DUP2 #09 SFT2 EOR2 DUP2 #09 SFT2 EOR2
@ -61,3 +66,17 @@
&seed $2 &seed $2
@prng2 ( -- number* )
( returns the next number in a (2^32-1)-long sequence )
( http://b2d-f9r.blogspot.com/2010/08/ 16-bit-xorshift-rng-now-with-more.html )
,&x LDR2
DUP2 #50 SFT2 EOR2
DUP2 #03 SFT2 EOR2
,&y LDR2 DUP2 ,&x STR2
DUP2 #01 SFT2 EOR2 EOR2
,&y STR2k POP
JMP2r
&x $2
&y $2