Random number generator

BlitzMax Forums/BlitzMax Beginners Area/Random number generator

Chroma(Posted 2005) [#1]
Still converting and trying to understand some C stuff to use in BMax. I looked up random generators in C and I'm still a bit lost. Consider the C code:
#define MWC ((s1 = 36969 * (v1 & 0xffff) + (v1 >>> 16)) << 16) ^ (s2 = 30963 * (v2 & 0xffff) + (v2 >>> 16));

I know that it makes random numbers. Also that it is called so forth:
i=(MWC>>8)&127

How would that translate to BMax? More specifically, the low and high constraints for the random number to be generated ie. i = Rand(low,high)
?


Scott Shaver(Posted 2005) [#2]
I think this is the translation you want. The global initialization of z and w are the seeds. The upper and lower bounds are controlled via the addition of the lower bound to the calculation modulo the larger bound.

'http://www.mathematik.uni-bielefeld.de/~sillke/ALGORITHMS/random/marsaglia-inline-c
'#define znew   (z=36969*(z&65535)+(z>>16))
'#define wnew   (w=18000*(w&65535)+(w>>16))
'#define MWC    ((znew<<16)+wnew )

Global z:Long=MilliSecs()'362436069
Global w:Long=MilliSecs()'521288629

For i=0 To 10
	Print ""+dorand(1,100)
Next

Function dorand:Long(small:Long,large:Long)
	z = 36969*(z & 65535)+(z Shr 16)
	w = 18000*(w&65535)+(w Shr 16)
	Return small+(((z Shl 16)+w) Mod large)
End Function