Random Numbers
Seen this:
http://www.worldofspectrum.org/forums/s ... hp?t=23070
?
If it's smaller, faster or better, you might want to consider
For posterity, an 8 bit result with 32 bit seed that passes diehard test for 32 bit entropy:
FUNCTION FASTCALL randomBase () as uByte
ASM
random: ld hl,$A280 ; xz -> yw
ld de,$C0DE ; yw -> zt
ld (random+1),de ; x = y, z = w
ld a,e ; w = w ^ ( w << 3 )
add a,a
add a,a
add a,a
xor e
ld e,a
ld a,h ; t = x ^ (x << 1)
add a,a
xor h
ld d,a
rra ; t = t ^ (t >> 1) ^ w
xor d
xor e
ld h,l ; y = z
ld l,a ; w = t
ld (random+4),hl
ret
END ASM
END FUNCTION
FUNCTION FASTCALL randomFixed() as FIXED
ASM
call random
push AF
call random
ld l,A
POP AF
ld h,a
ld d,0
ld e,d
END ASM
END FUNCTION
FUNCTION t() as uLong
asm
DI
LD DE,(23674)
LD D,0
LD HL,(23672)
EI
end asm
end function
DIM time,endtime as uLong
REM Print the base random 8 bits:
print randomBase()
REM print as a decimal, like basic's random number:
print randomFixed()
REM Works the same way:
print int(randomFixed()*10)
pause 1
pause 0
cls
dim n as uInteger
REM how does this look as a random dot spread?
time=t()
for n=1 to 30000
plot rnd*255,rnd*192
'print at 0,0;INT(rnd)
next n
REM (it looks good)
endtime=t()
print endtime-time;" Frames"
pause 1
pause 0
cls
REM how does this look as a random dot spread?
time=t()
for n=1 to 30000
plot randomFixed()*255,randomFixed()*192
'print at 0,0;INT( randomFixed() )
next n
REM (it looks good)
endtime=t()
print endtime-time;" Frames"
britlion wrote:Did you try it?
I tweaked it here to do 10,000 on rnd and time it, then do 10,000 on random and time it.
I recommend turning emulator up to full speed!
boriel wrote:RND is used very often in games, and the memory footprint looks not very significantAlso, for compatibility's sake, we should consider RND returning Float (yes, should be a bit slower)
Users browsing this forum: No registered users and 0 guests