Re: 32-bit random numbers
- Posted by Al Getz <Xaxo at aol.com> Jul 03, 2004
- 580 views
Hello again, I have to agree with Rob, that the probability distribution *might* not be acceptable for some things, but i dont think Derek's solution was that far off. I'd make a slight change to improve it a little and i'd use if myself for everything except applications that require encryption of data:
constant n=#10000 function Rand32() --no reason why you cant make this more inline... atom a,b,c a=rand(n)-1 b=rand(n)-1 c=a*n+b return c end function
P.S. We havent heard from Wolf yet??? Take care, Al And, good luck with your Euphoria programming! My bumper sticker: "I brake for LED's" Robert Craig wrote: > > Tommy Carlier wrote: > > How to create a 32-bit random integer between 1 and n (n = 32-bit integer > > atom): > > > > function rand32(atom n) > > if n <= #3FFFFFFF then return rand(n) > > else return rand(n - #3FFFFFFF) + rand(#3FFFFFFF) - 1 > > end function > > > > PS: if you leave out the -1 at the end, the number 1 will never be chosen. > > That will work, but keep in mind that the probability distribution > of the sum of two random numbers is not flat. For example, > if you roll two dice (1-6), the sum is far more likely to > be 7 than 12. So what would I do? I'm not sure, but I guess you > could take two random 16-bit numbers and concatenate them into > a 32-bit number. I'd still have some slight worries though, > since the two 16-bit numbers would be consecutive numbers > from the same random number generator. Are they truly independent? > Of course not, but then rand() is really just giving you > "pseudo-random" numbers anyway. > > Regards, > Rob Craig > Rapid Deployment Software > <a href="http://www.RapidEuphoria.com">http://www.RapidEuphoria.com</a> >