User Tools

Site Tools


16-bit PRNG

by White Flame

See Small, fast 8-bit PRNG for details of the algorithm. This should randomly iterate over a full 0000-ffff range, instead of the 0001-ffff range that the more common Galois LFSR covers.

Code is not well tested, though.

 lda seed
 beq lowZero ; $0000 and $8000 are special values to test for
 ; Do a normal shift
 asl seed
 lda seed+1
 bcc noEor
  ; high byte is in .A
  eor #>magic
  sta seed+1
  lda seed
  eor #<magic
  sta seed
 lda seed+1
 beq doEor ; High byte is also zero, so apply the EOR
           ; For speed, you could store 'magic' into 'seed' directly
           ; instead of running the EORs
 ; wasn't zero, check for $8000
 beq noEor ; if $00 is left after the shift, then it was $80
 bcs doEor ; else, do the EOR based on the carry bit as usual
 sta seed+1

All 2048 magic EOR numbers that give a 16-bit PRNG of period 65536:

base/small_fast_16-bit_prng.txt · Last modified: 2017-10-26 07:26 by white_flame