0

I was experimenting with Random class in Java and, as many others here on Stack Overflow, I noticed that using a seed will always give the same result (and it's okay, seems logic), but I still have a (theoretical) question: what's the real difference between giving a chosen seed and System.currentTimeMillis as seed? Testing the same program, the same time of the day, won't give me the same results after multiple tries?

I know it's really hard for me to obtain the same exact result, but if my program were to be used by millions, or billions of people, few could get the same results in terms of randomization, for example: Imagine a getpoints.exe where you have to click a bottom and get a random amount of points depending on the time of the day. Using Random(System.currentTimeMillis) won't be random over all because two people clicking at the same exact time will get the same amount, so the result won't be really random. The same applies if 1mln people press it simultaneously.

So the original question: are there pseudo-real but more accurate ways to get random numbers?

  • 1
    "*Testing the same program, the same time of the day, won't give me the same results after multiple tries?*" - If the program is started at the same exact moment in time on two different machines, it will yield the same result. This is why there is no real randomness without some external entropy (i.e. some external device, in some cases a microphone with the gain turned up to max may suffice), and this is why those algorithms are classified pseudo-random. Given the same seed, they will always output the same sequence. – Turing85 Dec 30 '17 at 23:00
  • 1
    "Testing the same program, the same time of the day, won't give me the same results after multiple tries?" - eventually, yes. But the seed is not correlated with the time of day. It is typically produced from various sources of entropy (true randomness) available to the system. – Stephen C Dec 30 '17 at 23:10
  • @Turing85 The example of the microphone is very interesting, I never thought about using external true random scenarios to implement random numbers –  Dec 30 '17 at 23:12
  • @StephenC I did not know that Random in that case would have depended from entropy sources –  Dec 30 '17 at 23:13
  • 2
    Just the seed! But the **real** problem with `Random` is that the PRNG is simple generator with poor statistical properties. It is a linear congruential PRNG with a relatively short cycle length. LC implies strong autocorrelation. I recommend you look at the references in the javadocs for `Random` ... to understand what that actually means. – Stephen C Dec 30 '17 at 23:16
  • 2
    @dulindraxe within Java's implementation of `Random`'s default constructor, no external entropy other than `System.nanoTime()` is used. There is some magic happening with a magic constant and some bit-operations, but no additional entropy. Thus, my initial statment holds: If you start the program on two different machines at the same exact point in time (and under the assumption that the clocks of this machines are in sync), you will get the same result. As for the external entropy factors... there are even services selling entropy in form of true random numbers. – Turing85 Dec 30 '17 at 23:18
  • 1
    Hmmm ... maybe I am confused about the source of seed for `Random`. I didn't check. But it is actually a red-herring. The real problems are the stats, and the fact that there are effectively only 48 bits of seed and a cycle length of 2^48. – Stephen C Dec 30 '17 at 23:19
  • @Turing85 I'd really like to try the external-source-of-randomness (maybe with an Arduino and a mic?). –  Dec 30 '17 at 23:24
  • 1
    If you want a better PRNG (and better seeding) use `SecureRandom`. – Stephen C Dec 30 '17 at 23:26
  • Nevermind if you got confused @StephenC, I have all the night to google about! And I didn't even know about the existence of `SecureRandom` to be sincer... –  Dec 30 '17 at 23:28

0 Answers0