Re: How to generate random 32bit integers
- Posted by cp Feb 17, 2011
- 1750 views
DerekParnell said...
cp said...
I have a need to generate random numbers in a range of 1 to n. N varies but may be as high as max 32 bit Uint.. 4,294,967,295.
I looks like I can do this with v 4.0 but unfortunately my app is in 3.1 and I'm not able to convert and test in the short term.
Speed is more important than distribution. It looks like the windows api may have a call for getting random numbers however there may be something better (easier/faster) coded in euphoria?
Thanks
Try ...
function rand32() atom A,B A = rand(#10000) - 1 B = rand(#10000) - 1 return (A * #10000 + B) -- Returns an ATOM not an INTEGER, in the range 0 to #FFFFFFFF end function
cp said...
This is close. I can't allow the return to be zero so It looks like I could just remove the -1 part so that I never get a 0 result? Also since each time I do a run I have a differnt max value so I could replace #10000 with N where n = the maximum of the range?