Monday, 2 November 2015

Random Number Generator to Generate Different Numbers

C# Random method is initialized using the clock. That's why it generates same value every time it calls. Here is the solution to generates different numbers:


private static readonly Random random = new Random();
private static readonly object syncLock = new object();

public static int RandomNumber(int min, int max)
{
    // synchronize
    lock (syncLock)
    {
        return random.Next(min, max);
    }
}


No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...