第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何清除系統(tǒng).隨機(jī)種子?

如何清除系統(tǒng).隨機(jī)種子?

C#
哆啦的時光機(jī) 2022-08-20 15:28:33
我使用 System.Random 函數(shù)為隨機(jī)數(shù)創(chuàng)建/生成種子,然后使用 Next() 為后續(xù)數(shù)字創(chuàng)建/生成種子,但與 c++ 非常相似,“rng”每次都能為我提供相同的隨機(jī)數(shù)結(jié)果。但是在c ++中,這是通過清除c ++中的種子來解決的,所以我想知道這在c#中是否也可能?
查看完整描述

1 回答

?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗 獲得超4個贊

您很可能每次都使用一個新實例。不應(yīng)以可重復(fù)方式實例化。Randomnew Random(seed_here)


Random r = new Random(); //Do this once - keep it as a (static if needed) class field 

     

for (int i = 0; i < 10; i++) {

     Console.WriteLine($"{r.Next()}");

}

更新

下面是一個更復(fù)雜的示例:


class MyClass

{

    //You should use a better seed, 1234 is here just for the example

    Random r1 = new Random(1234); // You may even make it `static readonly`


    public void BadMethod()

    {

        // new Random everytime we call the method = bad (in most cases)

        Random r2 = new Random(1234); 


        for (int i = 0; i < 3; i++)

        {

            Console.WriteLine($"{i + 1}. {r2.Next()}");

        }

    }


    public void GoodMethod()

    {


        for (int i = 0; i < 3; i++)

        {

            Console.WriteLine($"{i+1}. {r1.Next()}");

        }

    }

}

class Program

{

    static void Main(string[] args)

    {

        var m = new MyClass();


        m.BadMethod();

        m.BadMethod();

        m.GoodMethod();

        m.GoodMethod();


    }

}

輸出


1. 857019877

2. 1923929452

3. 685483091    

1. 857019877  <--- Repeats

2. 1923929452

3. 685483091

1. 857019877

2. 1923929452

3. 685483091

1. 2033103372 <--- Phew! It's a new number

2. 728933312

3. 2037485757


查看完整回答
反對 回復(fù) 2022-08-20
  • 1 回答
  • 0 關(guān)注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號