Generate Random number in C#
C# provides random class which is defined in System.random namespace which is used to generate random numbers.
Methods
- Next()- Returns a random non negative integer value.
- Next(int minvalue,int maxvalue)- Returns a random number between the specified range. Exclusive of the maxvalue
- NextBytes()- Fills the array of specified array of bytes with random numbers.
- NextDouble()- Returns a random number between 0.0 to 1.0
Example
Random number =new Random();
//To generate random integer
int any_integer =number.Next();
//Generate random integer from 1-12
int mont=number.Next(1,13);
//Generate a random double number between 0.0 to 1.0
double numb =number.NextDouble();
//Generate random numbers in specified array
Byte[] b = new Byte[10];
number.NextBytes(b);
//Print the array
for(int m=0;m<10;m++)
Console.Write(b[m]+"\t");
Generate GUID number
Syntax: Guid.NewGuid();
Example:
string key = Guid.NewGuid().ToString();
Console.WriteLine(key);
Click on the button to learn about bat file
No comments:
Post a Comment