您的位置:首页 > 编程语言 > Java开发

java random() 和 random(seed)的区别

2016-08-22 20:21 337 查看

java random() 和 random(seed)的区别

来自SO的回答:

The answer above sums it up clearly. As per java api docs from oracle, the first constructor

Random()


“Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor. “

The seed is probably a derivative of the current time, or the current time itself. That should be enough to be “very likely to be distinct from any other invocation”. Which, in essence, is most likely what you need, most of the time.

So why have another constructor that takes a seed?

Simply put, if you want to generate the same set of random numbers over and over, you use the same seed on your Random constructor. This is useful when doing experiments on different control sets, and you don’t want to bother creating your own table of random inputs, but still want the same set of random input on a different experiment/control set.

译:

上述的答案已经总结很清晰了。就每个oracle java api docs中说的,第一个

Random()


创造一个随机数产生器。这个构造函数将采用一个最可能去其他调用这个方法区分开来的seed。

这个seed可能是来自当前的时间或者就是当前的时间,应该是足够可以去其他过程中区分开来的,基本上,这个就是我们最需要用的。

所以为什么还需要seed的构造函数?

简单来说,如果你想产生一样的set多次,就就需要使用相同seed进行构造函数。这是很有用的,尤其实验不同的多个值反馈, 你并不像繁琐的创建你自己的随机输入, 但是仍然希望控制这个集合是一定的。

Note:

使用seed的参数,如果seed一样,那么只要调用next的次数相同,产生的值就是相同的。JDK的没看明白
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: