jeudi 21 septembre 2017

C# Threading - All threads are calculating same random value , why is that? [duplicate]

This question already has an answer here:

program is asking 10 threads and launching it on a function which is printing random value , but all those threads are printing the same Random value .

My friends says that it is due to thread are using rand calculation at same CPU clock so that is why. Is he right

And if there is a solution for it ?

      private DateTime randomNum()
    {

         Random r = new Random();
        int rInt = r.Next();
    }

button_click1

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

            res[i] = delcall.BeginInvoke(null, null);
        }
        //int[] num = new int[numofThreads];
        for (int i = 0; i < numofThreads; i++) {
            DateTime dt = delcall.EndInvoke(res[i]);
            richTextBox1.AppendText(dt + "\n ");
        }




Aucun commentaire:

Enregistrer un commentaire