Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Math.random period

Tags:

java

random

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

like image 475
Johannes Aronsson Avatar asked Feb 16 '17 19:02

Johannes Aronsson


People also ask

How does the math random work in Java?

random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread.

Is Java math random really random?

random() is based on java. util. Random , which is based on a linear congruential generator. That means its randomness is not perfect, but good enough for most tasks, and it sounds like it should be sufficient for your task.

How do you write a range in math randomly?

random() function: The Math. random() function is used to return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive).


1 Answers

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

like image 168
vaek Avatar answered Nov 07 '22 06:11

vaek