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的没看明白
相关文章推荐
- JAVA中Random类的Random r=new Random()和Random r=new Random(seedValue)的区别
- java Random seed
- java区别2个random类
- 我也来谈谈Java: Random.nextInt() 和Math.random()的区别
- Java——Random random = new Random(long seed)
- 《thinking in java》笔记2-Random()和seed
- java中Math.random()与java.util.random()的区别
- 【Java】java.util.Random()/Random(long seed)
- java中Random(long seed)方法与rRandom()方法的使用产生随机数
- java中随机数Random和ThreadLocalRandom()用法与区别
- Java 解惑:Random 种子的作用、含参与不含参构造函数区别
- Java: Random.nextInt() 和Math.random()的区别
- java中Random类和Math类中的random方法的联系和区别
- 浅谈java中Math.random()与java.util.random()的区别
- Java 解惑:Random 种子的作用、含参与不含参构造函数区别
- new Random(seed)和Random()区别
- Java 解惑:Random 种子的作用、含参与不含参构造函数区别
- java中Math.random()与java.util.random()的区别
- java基础_函数Math.random()和Random的使用及区别
- Java: Random.nextInt() 和Math.random()的区别