dimanche 11 décembre 2016

Is it good practice to use static methods for generating random numbers?

I've written the following code for generating random float and double numbers.

public static class Statics
{
    private static readonly Random random = new Random();
    private static readonly object syncLock = new object();

    public static double getRandomDouble(double min, double max)
    {
        lock (syncLock)
        {
            return random.NextDouble() * (max - min) + min;
        }
    }

    public static float getRandomFloat(float min, float max)
    {
        lock (syncLock)
        {
            return (float)random.NextDouble() * (max - min) + min;
        }
    }
}

Is it OK that I'm using static class and methods for generating random numbers?

My program relies heavily on these methods so I want to make sure that the produced numbers are indeed random.




Aucun commentaire:

Enregistrer un commentaire