32

While conducting some experiments in Java, my project supervisor reminded me to seed each iteration of the experiment with a different number. He also mentioned that I should use prime numbers for the seed values. This got me thinking — why primes? Why not any other number as the seed? Also, why must the prime number be sufficiently big? Any ideas? I would've asked him this myself, but its 4am here right now, everyone's asleep, I just remembered this question and I'm burning to know the answer (I'm sure you know the feeling).

It would be nice if you could provide some references, I'm very interested in the math/concept behind all this!

EDIT:

I'm using java.util.Random.

FURTHER EDIT:

My professor comes from a C background, but I'm using Java. Don't know if that helps. It appears that using primes is his idiosyncrasy, but I think we've unearthed some interesting answers about generating random numbers. Thanks to everyone for the effort!

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43
  • 9
    I would be really interested to see if he wasn't full of crap. Particularly since for something like `java.util.Random`, this reduces the possible sequences by a LOT over using the default time-based seed. – Mark Peters Mar 16 '11 at 20:26
  • 4
    It sounds like he's confusing it with crypto stuff. – SLaks Mar 16 '11 at 20:28
  • @SLaks yeah thats what i thought at first. perhaps i might have to wait till tomorrow for his explanation (if there is any). – Dhruv Gairola Mar 16 '11 at 20:32
  • 1
    If it is for an experiment then you want them to be reproducible and doing it @Mark Peters way (the time based way) would be bad. The prime numbers could be a way to get a varied yet deterministic spread of start values. For a good random number generator just using 1,2,3... should do as well. – josefx Mar 16 '11 at 20:37
  • @josefx: Using time doesn't make it non-reproducible. You can just output the seed at the beginning, record it, and if you want to reproduce the test later override the default. – Mark Peters Mar 16 '11 at 20:39
  • 1
    Regarding what I said earlier about the use of primes narrowing the domain over using the time...neither is relevant. There are many times more primes in the domain of a long as there are milliseconds in the next million years. We won't be exhausting either any time soon. So I don't think using a prime sequence for seed values will affect entropy, but I don't think there's a good reason for it either. – Mark Peters Mar 16 '11 at 20:43
  • *I'm using java.util.Random.*, so the seed doesn't matter as long it's not predictable (using prime actually does so) – bestsss Mar 16 '11 at 21:51
  • 2
    @Mark, java.util.Random does not use `System.currentTimeMillis()` but smth like: `public Random() { this(++seedUniquifier + System.nanoTime()); } private static volatile long seedUniquifier = 8682522807148012L; ` – bestsss Mar 16 '11 at 21:55

3 Answers3

24

Well one blink at the implementation would show you that he CAN'T have any reason for that claim at all. Why? Because that's how the set seed function looks like:

synchronized public void setSeed(long seed) {
    seed = (seed ^ multiplier) & mask;
    this.seed.set(seed);
    haveNextNextGaussian = false;
}

And that's exactly what's called from the constructor. So even if you give it a prime, it won't use it anyhow, so if at all you'd have to use a seed s where (s^ multiplier) & mask results in a prime ;)

Java uses a usual linear congruency method, i.e.:

x_n+1 = (a * x_n + c) mod m with 2 <= a < m; 0 <= c < m.

Since you want to get a maximal periode, c and m have to be relatively prime and a few other quite obscure limitations, plus a few tips how to get a practically useful version. Knuth obviously covers that in detail in part2 ;)

But anyhow, the seed doesn't influence the qualities of the generator at all. Even if the implementation would be using a Lehmer generator, it would obviously make sure that N is prime (otherwise the algorithm is practically useless; and not uniformly distributed if all random values would have to be coprime to a non prime N I wager) which makes the point moot

Voo
  • 29,040
  • 11
  • 82
  • 156
  • that's the correct answer :D. a prime seed has no influence on the default java.lang.Random. – bestsss Mar 16 '11 at 21:53
  • im quite convinced that using primes is some sort of idiosyncrasy of his since its quite apparent that primes don't affect anything special. also, your last point about the Lehmer generator makes sense- it seems to require primes to be considered a Lehmer generator in the first place! good effort! – Dhruv Gairola Mar 17 '11 at 10:13
  • In fact, I would argue that using primes would hurt your random since you will probably but pulling from a limited set of pre-determined prime numbers thus meaning you will have an arbitrary number of seeds. – Chris J Mar 17 '11 at 10:27
  • @Chris well, the way my experiment runs, i only require a fixed number of seeds each time. hmm, im not convinced that using primes would hurt my random for this case. i am convinced, however, that they would make no difference. – Dhruv Gairola Mar 17 '11 at 10:53
22

If the generator is a Lehmer generator, than the seed and the modulus must be co-prime; see the wiki page. One way to ensure they are co-prime is to start with a prime number.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
  • +1 For pointing out a case of where it does make a difference :p –  Mar 16 '11 at 20:31
  • 4
    I'm guessing that the prof doesn't really know if the generator is a Lehmer generator, but often such advise comes about with the following pattern: Something was true for a specific issue, and then get reapplied to situations where it isn't needed. Unless this pattern actually causes something to break, it can be quite some time before a person (or a professor) bothers to check up on the validity of their requirements. – Edwin Buck Mar 16 '11 at 20:33
  • 4
    An undocumented requirement that the seed is coprime to some secret value would be a *severe* implementation flaw in `java.util.Random`... – Steve Jessop Mar 16 '11 at 20:36
  • 1
    The OP did not state that java.util.Random was the generator in question. – Doug Currie Mar 16 '11 at 20:37
  • @Doug +1 very cool! im not using the Lehmer generator though, but this is certainly an impressive effort (@Doug im using java.util.Random). might just have to ask him tomorrow.. sigh.. hope i wake up on time.. – Dhruv Gairola Mar 16 '11 at 20:38
  • okay my above comment has two @'s. kindly ignore the second @ (i need sleep!) – Dhruv Gairola Mar 16 '11 at 20:46
  • While that's theoretically true every implementation will obviously use a prime number for N because it's virtually impossible for the whole sequence of numbers to be coprime to N. Luckily 2^31-1 is a prime so no real problem there. – Voo Mar 16 '11 at 21:27
12

If you are talking about java.util.Random, or one of its subclasses in the Oracle runtime, there's no reason for this. It's just a whim of your supervisor.

erickson
  • 265,237
  • 58
  • 395
  • 493