10

I'm working on a big school project about random numbers, but I can't find the period for Math.random(). I have version 7.0.800.15 installed and I'm working an a windows 10 computer. I've tried determining the period with a simple program which saves the first values of:

double num = Math.random(); 

in an array and then loops until it finds the same values in a row again, thus a period would have passed, but with no result, the period is too long.

So my question is: What is the period of Math.random() on my version? Or: Is there a way to determine the period using a simple program?

Edit: took away a source pointing to a page about JavaScript, it was not relevant

Ziem
  • 6,579
  • 8
  • 53
  • 86
  • What do you mean by period? The number of calls until the same result is returned? – Izruo Feb 16 '17 at 19:33
  • Is your question about Java or Javascript? The link is talking about Javascript. – Kayaman Feb 16 '17 at 19:38
  • 1
    @Izruo By period i mean just that, the number of calls until the same digits start repeating – Johannes Aronsson Feb 16 '17 at 19:39
  • I do now realize that the article is about javaScript, my bad, it was the only source I could find, thanks. My question is about Java @Kayman – Johannes Aronsson Feb 16 '17 at 19:40
  • And when you need true (or at least cryptographically safe) randomness, you use `SecureRandom`. – Kayaman Feb 16 '17 at 19:45
  • @XinHuang That was not the question, what i meant was: What is the period, as in: when will the numbers start repeating themselves. For example in php the period is 32767 which means that after that many numbers the exact same numbers in the exact same order will appear again – Johannes Aronsson Feb 16 '17 at 19:46
  • 1
    Note that in a true random number generator you might not be able to tell what the period is just by printing the values. For example,it is possible (but not very probable) that the values are 3, 2234, 96, 3, 2234, 96, 3, 2234, 96, ... where you might incorrectly conclude that the period is 3. – FredK Feb 16 '17 at 19:50
  • Welcome to Stack Overflow by the way. Superb question, incidentally - wish I could upvote it more than once. – EJoshuaS - Stand with Ukraine Feb 16 '17 at 20:18

3 Answers3

5

Java's Math.Random uses a linear congruential generator with a modulus of 2^48. The period of such pseudorandom generator with well-chosen parameters is equal to the modulus. Apparently the parameters in Java are sanely chosen, so in practise the period is 2^48.

Sources: https://en.wikipedia.org/wiki/Linear_congruential_generator http://www.javamex.com/tutorials/random_numbers/java_util_random_algorithm.shtml#.WKX-gRJ97dQ

vaek
  • 321
  • 1
  • 2
4

The wiki on linear congruential generator cites Java (java.util.Random) as having a modulus of 248. That is likely the period but you may need to read more about these types of random generators.

This question (How good is java.util.Random?) also cites the same period.

Community
  • 1
  • 1
Justin Hellreich
  • 575
  • 5
  • 15
2

Just to add to the other answers and to comment a little more generally on random number generators and writing a program to determine what the period is, beware of the Birthday Paradox and the Gambler's Fallacy. If you generate some value x, the next number is still just as likely to be x as any other number, and the number of numbers you need to generate before you're likely to have a duplicate is actually surprisingly small (meaning that you could, in principle, start seeing some duplicates before the end of the period, which complicates writing a program to test this).

The probability of a duplicate for probabilities up to 50% or so can be approximated by sqrt(2m * p(n)) where p(n) is the probability you're trying to calculating and m is the number of choices. For a 32-bit integer, sqrt(2m * p(n)) = sqrt(2 * 2^32 * 0.5) = sqrt(2^32) = 65,536. There you have it - once you generate 65,536 numbers there's approximately a 50-50 chance you've generated a duplicate.

Once you've generated 2^32 + 1 values, the Pigeonhole Principle specifies that you must have generated at least one duplicate (assuming, of course, that you're generating a 32-bit number).

You may also be interested in this question on whether you can count on random numbers to be unique.

Community
  • 1
  • 1