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

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的没看明白

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值