Re: [問題] 亂數
※ 引述《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
推
07/29 17:35, , 1F
07/29 17:35, 1F
討論串 (同標題文章)