// This works surprizingly well. ... I am shocked at how
// good the distribution is. Take a peek at the following image
// http://gyazo.com/83546e60d2dfeaa97b0ddc41f4edc269.png
// I got the code from this site and ported it to ARMbasic.
// http://www.bobwheeler.com/statistics/Password/MarsagliaPost.txt
/* Adapted to 31bit space since AB uses singed ints and shifting
signed negatives retains the sign vs. treating them like unsigned... */
/* Global static variables: */
dim _z, _w as integer // used by mwc random number generator
function znew
z=36969*(z and $ffff)+(_z>>16 and $ffff)
return $7fffffff and _z<<16
endfunction
function wnew
w=18000*(w and $ffff)+(_w>>16 and $ffff)
return _w and $ffff
endfunction
function mwc
return (znew+wnew)
endfunction
function rand as single
dim u as integer
// Produces a random sample from the open interval (0, 1).
// The method will not return either end point.
// 0 <= u < 231
u = mwc
// The magic number below is 1/(231 + 2).
// The result is strictly between 0 and 1, exclusive of either.
return (u + 1.0) * 4.656612868740580000E-10
endfunction
sub init_globals
' _z = 362436069 ' default static seed
_z = timer and $7fff0000 '<- use a static seed if you want repeatability...
_w = 521288629
endsub
main:
dim x as integer
init_globals
// this loop creates 100000 pairs of random numbers
// I did this to copy and paste into excel and then do a graph
// to demonstrate the distribution
// a image of the graph is shown here:
// http://gyazo.com/83546e60d2dfeaa97b0ddc41f4edc269.png
for x = 1 to 100000
Print rand, rand
next x
end