Wednesday, August 10, 2011

Generate Random Numbers using C#

Random randNum = new Random();
randNum.Next();

That's all it takes. The creation of a Random object (of the System.Random class) and calling its Next() method, which is going to return a non-negative random number as an integer.
EX:
Random randNum = new Random();
randNum.Next(108); // No larger than 108
randNum.Next(1, 108); // No larger than 108, no smaller than 1

No comments:

Post a Comment

Implementing Service Locator (To Resolve Dependency)

using System; /// <summary> /// Summary description for Class1 /// </summary> public class serviceLocator {     public s...