1

I want to display random rumbers with many tasks but the result is I m getting duplicated numbers

for (int i = 0; i < 500; i++)
{
    Task.Factory.StartNew(() =>
    {
        string a = new Random().Next(0, 999999).ToString();
        richTextBox1.Invoke(new Action(() =>
        {
            richTextBox1.AppendText(a + "\n");
        }));
    });
}

691241 691241 691241 691241 691241 691241 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621 527621

I have also another question about Task and Threads
Is it important that the class is not static, for example if I want to send multiple POST queries with threads / tasks?

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • 2
    Read the docs: _The Random() constructor uses the default seed value. This is the most common way of instantiating the random number generator. In .NET Framework, the default seed value is time-dependent. In .NET Core, the default seed value is produced by the thread-static, pseudo-random number generator._ – Ian Mercer Mar 28 '20 at 21:41

2 Answers2

1

You need to declare the Random instance OUTSIDE of the FOR loop....

Then reuse that instance inside. You might also want to "lock" the call to the random class so that two calls wont happen at the same time.

        var synRoot = new object();
        var rand = new Random();
        for (int i = 0; i < 500; i++)
        {
            Task.Factory.StartNew(() =>
            {
                lock(syncRoot)
                {
                    //lock to avoid multiple threads calling at the same time.
                    string a = rand.Next(0, 999999).ToString();
                    richTextBox1.Invoke(new Action(() =>
                    {
                        richTextBox1.AppendText(a + "\n");
                    }));
                }

            });
        }   

The reason is that the Random class seeds itself using the resolution of the internal clock... so this means that if 2 or more tasks create a new instance within that resolution window both instances will have the same SEED.

In order to avoid this you need to reuse a single instance.

Jonathan Alfaro
  • 4,013
  • 3
  • 29
  • 32
  • okay that works well. and what If I make for example a own class (non static) that class sends Post Request to a server should I also create the instance outside the loop? – schoko Bong Mar 28 '20 at 21:46
  • @schokoBong depends on the scope of the "randomness" if you want it to be random amongst all requests.... or may be just the user session... or some other criteria. But thep point is you have to decide what the scope of the randomness is. – Jonathan Alfaro Mar 28 '20 at 21:59
  • @schokoBong you might also want to add a lock statement to avoid other issues. If you think this answers your question please mark as answered. – Jonathan Alfaro Mar 28 '20 at 22:02
  • 1
    great thanxs I need to look more for lock/Threads/Task :D – schoko Bong Mar 28 '20 at 22:07
0

new Random() creates a Random object with default seed, actual time. If you create two randoms within a short time, they will have same seed. So create one Random object and use it in for loop.

galaxy001
  • 404
  • 5
  • 16