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
No because
new Random()
will init with current time as a seed. That said you should still preserve instance of therandom
and reuse it.You should only initialize the seed once and then reuse it:
Maybe.
Random
created without an explicit seed seeds itself based on current time. If you calledRandomNumber
rapidly enough you'd get the same number occasionally.Your intuition is correct, however. It's dumb to create a new
Random
object every time you need a new number. You should create a single instance and use it.It will give the same result when the method will be called often between short time intervals. This is because the
Random
s seed is initialized with the current time value. This is also the reason why many people have problem of kind that random is not random at all.BTW it is not Math.Random but System.Random
Following your edit, here is some information on how random is initialized. The information comes from the link above.