Sunday, July 12, 2009

What is the equivalent to Math.Random() in C# .net?

if you follow the top post it is not random for multiple calls in the same timespan. Use the following to fix that





System.Random objRandom = new System.Random(((int)((System.DateTime.No... % System.Int32.MaxValue))));








public int GetRandomNumber(int Low,int High) {





// Returns a random number,


// between the optional Low and High parameters





return objRandom.Next(Low, (High + 1));





}

What is the equivalent to Math.Random() in C# .net?
The following code returns a random number:











int num = random.Next();





The following code returns a random number less than 1000.











int num = random.Next(1000);





The following code returns a random number between min and max:











private int RandomNumber(int min, int max)


{


Random random = new Random();


return random.Next(min, max);


}


No comments:

Post a Comment