123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #ifndef included_rand
- #define included_rand
- randinit:
- ld a,r
- ld hl,seedsingle0
- xor (hl) \ ld (hl),a \ inc hl
- xor (hl) \ ld (hl),a \ inc hl
- xor (hl) \ ld (hl),a \ inc hl
- xor (hl) \ ld (hl),a \ inc hl
- xor (hl) \ ld (hl),a \ inc hl
- xor (hl) \ ld (hl),a \ inc hl
- xor (hl) \ ld (hl),a \ inc hl
- or 97 ;no particular reason
- or (hl) \ ld (hl),a
- ret
- rand:
- ;Tested and passes all CAcert tests
- ;Uses a very simple 32-bit LCG and 32-bit LFSR
- ;it has a period of 18,446,744,069,414,584,320
- ;roughly 18.4 quintillion.
- ;LFSR taps: 0,2,6,7 = 11000101
- ;323cc
- ;Thanks to Runer112 for his help on optimizing the LCG and suggesting to try the much simpler LCG. On their own, the two are terrible, but together they are great.
- ;Uses 64 bits of state
- ld hl,(seedsingle0)
- ld de,(seedsingle0+2)
- ld b,h
- ld c,l
- add hl,hl \ rl e \ rl d
- add hl,hl \ rl e \ rl d
- inc l
- add hl,bc
- ld (seedsingle0),hl
- ld hl,(seedsingle0+2)
- adc hl,de
- ld (seedsingle0+2),hl
- ex de,hl
- ;lfsr
- ld hl,(seedsingle1)
- ld bc,(seedsingle1+2)
- add hl,hl \ rl c \ rl b
- ld (seedsingle1+2),bc
- sbc a,a
- and %11000101
- xor l
- ld l,a
- ld (seedsingle1),hl
- ex de,hl
- add hl,bc
- ret
- #endif
|