Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Numbers
#3
We haven't looked at this in a while.

Here's some test code:

Code:
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"

Basically, more than twice as fast in all cases.

I think the clincher, for me, is that even

Code:
print at 0,0;INT(CAST(FLOAT,randomFixed()))

- which forces a full floating point result, like rnd has, comes back 50% faster.

I also tried it with my planet proggy - <!-- l --><a class="postlink-local" href="http://www.boriel.com/forum/bug-reports/byte-loops-solved-t757.html#p3229">bug-reports/byte-loops-solved-t757.html#p3229</a><!-- l --> - it comes out over twice as fast.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)