C# Random Generation – Guide

c++random

I have just passed this article online:
C# Corner and C# Corner and his article (a software developer with over 13 years of experience) recommended using System.Random as follows:

private int RandomNumber(int min, int max)
{
    Random random = new Random();
    return random.Next(min, max); 
}

Isn't that would give him the same number every time ??

Edit:

So my question will become: How does Random choose its seed? a constant or current time value?

Thanks

Best Answer

You should only initialize the seed once and then reuse it:

private Random random = new Random();

private int RandomNumber(int min, int max)
{
    return random.Next(min, max); 
}
Related Question