Random seed Math.random in Java

2019-01-20 10:53发布

In my code I use random numbers in different classes. How to define random seed? Can I define this seed for all the classes in the main code?

double rnd = Math.random();

1条回答
甜甜的少女心
2楼-- · 2019-01-20 11:55

You will probably want to use the special Random class. It gives you more control over the random numbers. To do this you first need to create a new random object.

Random generator = new Random(seed);

Then generate a new number by

double random = generator.nextDouble();

http://docs.oracle.com/javase/6/docs/api/java/util/Random.html

查看更多
登录 后发表回答