Reputation: 503
I'm generating many random numbers and I need a good function, since this doesn't help much:
public static class Randomizer
{
static Random random = new Random((int)DateTime.Now.Ticks);
public static int RandomInteger(int minimum, int maximum)
{
return random.Next(minimum, maximum + 1);
}
public static double RandomDouble()
{
return random.NextDouble();
}
}
When I use this class, my numbers are very often the same. Do you have any simple idea how I can improve the randomness of the output of the randomizer?
Upvotes: 3
Views: 14823
Reputation: 45101
Take a look into MiscUtil from Jon Skeet.
There is a static random class, that works without any problems.
By the way, do you need random or unique numbers?
Upvotes: 0
Reputation: 22016
The reason your numbers are the same is that you are creating a new Random every call to the class which means it will seed using the same number from the DateTime.Now.Ticks
You should make this a non static class. Instantiate it once and then call repeatedly the same function from the object
Upvotes: -4