A.D.A. Amiga Demoscene Archive

        Welcome guest!

  

  

  

log in with SceneID

  

Demos Amiga Demoscene Archive Forum / Coding / floating point random number generator

 

Author Message
dalton
Member
#1 - Posted: 20 Mar 2010 11:33
Reply Quote
Hi,

does anybody have a simple routine for generating floating point random numbers? Right now I use the an LFSR generator on the 68k, and move the result over to FPU and scale. It works but I think it's an ugly solution...
rload
Member
#2 - Posted: 22 Mar 2010 17:14
Reply Quote
Maybe you can just generate the mantissa bits of the float as ints with the usual rotate-subtract which is used in so many 4k intros and such to produce a float between 0 and 1. Then you can get the number in range by multiplying it up afterwards or use the fscale if you only need it in a po2 range.


random
move.l random_seed,d0
ror.l d0,d0
subq.l #1,d0 ; demoscene random
move.l d0,d1
and.l #(1<<24)-1,d1 ; random mantissa bits in the 23 low bits
or.l #exponent_bits_and_sign_bit,d1
fmove.s d1,fp0 ; load fp0 with d1's bits. not sure this is the syntax. no format conversion should happen
fmul.s #range,fp0
move.l d0,random_seed
rts
random_seed dc.l 1243462


I'm sorry this is probably really bad code, but maybe a discussion starter for better suggestions. Also it is not tested or assembled or anything.
rload
Member
#3 - Posted: 22 Mar 2010 17:17
Reply Quote
And come to think of it.. This is probably just the thing you are describing in your post :)
dalton
Member
#4 - Posted: 23 Mar 2010 17:29
Reply Quote
thanks for the reply!

it's almost the same, I do:

move.l seed, d0
rol.r d0,d0
addq.l #7,d0
move.l d0, seed
fmove.l d0, fp0
fmul.s #1/32768, fp0
; to get random number in range [-1,1]


I sort of like your idea of loading without format conversion though. I believe my code will have much poorer resolution in the floating point number.

don't ask me about the difference in ror/rol and addq/subq. I have no idea =)

 

  Please log in to comment

  

  

  

 

A.D.A. Amiga Demoscene Archive, Version 3.0