可能重複:
Why does it appear that my random number generator isn't random in C#?
How can I generate truly (not pseudo) random numbers with C#?創建一個真正的隨機
我創建了一個骰子游戲,骰子是基於一個百分點,至100。
public static void Roll()
{
Random rand = new Random((int)DateTime.Now.Ticks);
return rand.Next(1, 100);
}
但我不覺得這是一個基於當前時間的真正的隨機數。
如果我做
for (int i = 0; i < 5; i++)
{
Console.WriteLine("#" + i + " " + Roll());
}
,他們都將是相同的值,因爲DateTime.Now.Ticks
沒有改變,它的種子數量相同。
我想我可以生成一個新的隨機種子,如果種子是一樣的,由於當前的時間,但它並不覺得自己是個誠實的「再卷」
我應該怎麼做嘗試並複製一個接近真實/誠實的骰子卷?我應該使用RNGCryptoServiceProvider
類來生成卷嗎?
我建議閱讀下面的內容:http://csharpindepth.com/Articles/Chapter12/Random.aspx – Oded
這個問題在SO上有很多變種。這裏有一個...... [爲什麼看起來我的隨機數生成器在C#中不是隨機的?](http://stackoverflow.com/questions/932520/why-does-it-appear-that-my- random-number-generator-isnt-random-in-c)...標記爲僞裝。 (你應該只有一個你的Random的靜態實例,而不是每個Roll的新Random) – spender
什麼是「真正的隨機」? – Sandy