1. How to generate random 32bit integers
- Posted by cp Feb 17, 2011
- 1816 views
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
2. Re: How to generate random 32bit integers
- Posted by bill Feb 17, 2011
- 1786 views
generate 2 16-bit random numbers and use them for upper 16, lower 16?
3. Re: How to generate random 32bit integers
- Posted by DerekParnell (admin) Feb 17, 2011
- 1799 views
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
4. Re: How to generate random 32bit integers
- Posted by cp Feb 17, 2011
- 1751 views
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
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?
5. Re: How to generate random 32bit integers
- Posted by DerekParnell (admin) Feb 17, 2011
- 1725 views
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?
Ok then, try ...
function rand32(atom N) integer A integer B if N <= #3FFFFFFF then return rand(N) end if B = remainder(N, #10000) B = rand(B) A = floor(N / #10000) A = rand(A + 1) - 1 return ((A * #10000) + B) -- Returns an ATOM not an INTEGER, in the range 1 to N end function
6. Re: How to generate random 32bit integers
- Posted by DerekParnell (admin) Feb 18, 2011
- 1721 views
Ok then, try ...
Actually, do not try that last one of mine. I just quickly wrote that during my lunch break. Now that I'm home and thought it through, it has holes. Sorry.
Anyhow, here are two different ways that you can try...
-- This first function is used locally and is not suitable for your purposes. function r32() -- return a random number from 1 to power(2,32) return ((rand(#10000) - 1) * #10000) + rand(#10000) end function -- Method A: Calculate two random numbers and join them. global function rand_A(atom N) integer A integer B if N <= #3FFFFFFF then return rand(N) end if A = floor(N / #10000) B = rand(A + 1) - 1 if B != A then A = rand(#FFFF) else A = remainder(N, #10000) if A then A = rand(A) end if end if return ((B * #10000) + A) -- Returns an ATOM not an INTEGER, in the range 1 to N end function -- Method B: Calculate a random number such that 0 < R <= 1, then multiple that by N global function rand_B(atom N) atom a,b,temp atom A, B atom UL,LL if N <= #3FFFFFFF then return rand(N) end if UL = (N - 1.5) / N LL = 1.5 / N a = r32() / r32() b = r32() / r32() if a > b then temp = b b = a a = temp end if A = a / b if A > UL then return N elsif A < LL then return 1 end if B = floor(N * A) return B end function --------------------
The distributions are different in the two methods so your sampling might be better with one method rather than the other. You'll have to test which is more suitable.
7. Re: How to generate random 32bit integers
- Posted by DerekParnell (admin) Feb 18, 2011
- 1651 views
And here is another method ...
-- Method C: Generate a large (50-bit) random number and take the remainder using N. function rc(atom N) atom a,b if N <= #3FFFFFFF then return rand(N) end if b = rand(#3FFFFFFF) a = ((b - 1) * #100000) + rand(#FFFFF) return remainder(a, N) + 1 end function