Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the JDK have both Math.random() and the Random class?

Tags:

java

random

Is it just because of "large API syndrome" or generating random numbers that are more biased favored in some situations? If it was..I would think that controlling the bias-ness would be important.

like image 950
irwinb Avatar asked Dec 20 '11 00:12

irwinb


People also ask

What is math random () in Java?

random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0. . When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java. util. Random.

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.

Which package contains the random class in Java?

The Java Random class is a part of the java. util package and contains inbuilt methods to generate random numbers.

Which function returns a random number between 0.0 and 1.0 where 0.0 is inclusive but 1.0 is exclusive?

random() method. This method returns a pseudorandom positive double value between 0.0 and 1.0, where 0.0 is inclusive and 1.0 is exclusive. It means Math. random() always return a number greater than or equal to 0.0 and less than 1.0.


1 Answers

They're the same, really. Just a convenience method. Check the javadoc here. Additionally, you're able to re-seed by creating random objects, while Math.random() will use a static Random instance.

like image 137
Miquel Avatar answered Sep 21 '22 10:09

Miquel