Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generating "random" numbers that don't repeat for 2^48 period

Tags:

java

random

Basically I want to generate random numbers that won't ever repeat for a very long period (I don't want to use a sequence) like for example the LCG that java uses:

 synchronized protected int next(int bits) {
       seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
       return (int)(seed >>> (48 - bits));
 }

As I understand the seed in this case will only repeat after 2^48 calls to next is this correct?

so it is my understand that if I did a method like:

 synchronized protected long next() {
       seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
       return seed;
 }

The seed value is guaranteed not to repeat before 2^48 calls?

like image 960
Oscar Gomez Avatar asked Jun 24 '11 04:06

Oscar Gomez


People also ask

How will you generate a random number which does not get repeated?

We can use either the RAND or RANDBETWEEN functions for this. The RAND function is the fastest because we don't have to specify any arguments. The RAND function returns a random decimal number to the cell. Input the formula =RAND() in the first cell and double-click the fill handle to copy the formula down.


1 Answers

For reference, the parameters for the linear congruential generator implemented in java.util.Random are as follows:

a = 25214903917 = 7 x 443 x 739 x 11003
c = 11
m = 248
a – 1 = 25214903916

The period length is at most m if and only if all of the following are true:

  1. c and m and are relatively prime

  2. a – 1 is divisible by all prime factors of m

  3. a – 1 is a multiple of 4 if m is a multiple of 4

Yes, the period is 248. The problem is "that the low order bits go through very short cycles." The strong correlation between the low order bits of successive values significantly limits what you can do with them.

like image 147
trashgod Avatar answered Sep 27 '22 21:09

trashgod