Solved

Alter 32bit to 64bit in Gwenforweb's snippet

Posted on 2013-06-08
9
553 Views
Last Modified: 2013-06-10
Gwenforweb helped me earlier (here), she gave me a code snippets for 32bit integers. It solved my problem and work perfectly, but as Gwenforweb mentioned, the snippet was for 32 bit integers.

My specs changed, now I need 64bit. Can someone alter her snippets to work using 64-bit integers?

seed=15239;
for (i=0;i<1000;i++){
 seed =(86028157*seed + 142501)%1299689; 
 rand=seed/1299689; 
 print(rand);
}

Open in new window

0
Comment
Question by:volking
  • 5
  • 3
9 Comments
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 500 total points
ID: 39232389
A max 64 bit unsigned integer is

18446744073709551615

ie about 1.8E19

You want to make sure your numbers does not exceed this.

48205429  and 6607882123  are both primes so this should work nicely. And will do for signed integers as well.

seed=15239;
for (i=0;i<1000;i++){
 seed =(6607882123 *seed + 142501)%48205429;
 rand=seed/48205429;
 print(rand);
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 39232411
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 39232878
The Hull-Dobell Theorem states that for the generator

seed = (a*seed +c)  mod m

Then the random numbers generated will have a full period for all seed values if and only if

c and m are relatively prime,
a - 1 is divisible by all prime factors of m,
a - 1 is a multiple of 4 if m is a multiple of 4

The scheme

 seed =(86028157*seed + 142501)%1299689;

satisfies these conditions and is the one I have given you.


Examples
Products that use generators of this form and are easily applicable to 64 bits include:-

Numerical Recipes:-

               seed = (1664525 *seed +1013904223)  mod  2³²

Older versions of Visual Basic:-

              seed = (1140671485*seed +12820163)  mod 2^24

Vax's MTH$RANDOM from glibc:-

              seed = (69069*seed + 1) mod  2³²
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 84

Expert Comment

by:ozo
ID: 39232892
Full period isn't the only desideratum for a PRNG.
(a=0 satisfies the full period requirement)
You may also want to avoid correlations between numbers separated by lags
http://www.ncbi.nlm.nih.gov/pmc/articles/PMC285899/pdf/pnas00123-0038.pdf
or small periods in the low order bits.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 39232904
Ozo, If we want a World class Random Number generator then we should using a completely different scheme all together. The user wants a reasonable and easily implementable scheme that gives repeatable numbers. The scheme I have suggested satisfies Hull-Dobell and gives a reasonable distribution.

These simple linear congruence schemes are know to be a bit of an art form and most of the literature on the subject gives values that are know to work well from experience and testing. The three other schemes I have given are from professional products that clearly have proven their worth, and are easily implemented without bit operations.

If the questioner wants a professional high end simulation standard random generator, then we can have a discussion on that as well. But my guess is the generators been given here are of the form the OP is looking for. These still are really really good generators even if they are not world class.

When answering a question I feel it is important to pay attention to what the OP wants and gauge what level they are operating at; and as such respond with mathematics and discussion that is not unnecessary or confusing.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39232968
I don't know everything the questioner wants.
Perhaps the full period generator with a=1, c=1 would suffice.
Perhaps duplicating a particular professional product is desireable.  (although some professional products have had notoriously poor PRNG qualities)
Or the full period generators with a=86028157 c=142501 m=1299689
or a=5171 c=142501 m=34421satisfy the requirement of working using 64 bit integers,
so if those didn't satisfy the changed specs, it seems at least conceivable that improved spectral properties could be a consideration.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 39232993
"Perhaps the full period generator with a=1, c=1 would suffice."

I am gathering that is humor.
0
 
LVL 5

Author Closing Comment

by:volking
ID: 39233042
Thanks for staying on topic and answering the question. Sometimes, experts are so smart, they want to educate the world. I'm just a simple ol'redneck with a HS diploma who enjoys playing programmer. Your little snippet makes that possible.

Thanks Gwenforweb
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 39233573
Your welcome. :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question