作者necole236 (necole236)
看板java
标题Re: [问题] 乱数
时间Tue Jul 29 17:33:04 2008
※ 引述《adrianshum (Alien)》之铭言:
: ※ 引述《necole236 (necole236)》之铭言:
: : 使用乱数的时候如果不给seed
: : 那麽 java 预设值是利用执行当时的时间去当作 seed
: : 假设有两个骰子物件,如果他们是同时被初始化,因此有着相同的seed
: : 那麽理论上这两个骰子会出现一模一样的乱数序列
: : 该怎麽用程式证明这一点呢?
: : 还是我的想法有错误呢?
: : 因为我做的骰子,没有办法同时初始化 @_@ ?
: 首先, 如 PsMonkey 大在推文所说, 你两个
: Random obj create 的时间肯定会有差距.
: 你怎样证明你是同时初始化两个 Random Obj?
: 其次, 根据 API doc, Random() 这个 constructor
: 只是说会设一个和其他 invocation 很可能会不同的
: 值作 seed (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), 并没有说只是拿当前时间作 seed.
: 要是想证明同 seed 会有一样的 random sequence,
: 为什麽不乾脆自己设一样 seed 就算了?
: alien
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#Random()
Random
public Random()
Creates a new random number generator. Its seed is initialized to a value
based on the current time:
public Random() { this(System.currentTimeMillis()); }
Two Random objects created within the same millisecond will have the same
sequence of random numbers.
我看到的定义是这样子写的
他并没有说除了当前时间之外,还做了什麽「会设一个和其他 invocation
很可能会不同的值作 seed」
假设你是对的,那他是如何设定一个不同的值做 seed ?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 71.170.204.16
1F:推 godfat:因为你看的版本太旧了 XD 07/29 17:35