Random seed Math.random in Java

随声附和 提交于 2019-11-27 07:51:26

问题


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:


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



来源:https://stackoverflow.com/questions/17445813/random-seed-math-random-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!