Generate Random Number in C#


Generate Random number in C#



C# provides random class which is defined in System.random namespace which is used to generate random numbers.


 Methods

  1.  Next()- Returns a random non negative integer value.
  2.  Next(int minvalue,int maxvalue)- Returns a random number between the specified range.         Exclusive of the maxvalue 
  3.  NextBytes()- Fills the array of specified array of bytes with random numbers.
  4.  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

GUID represents a globally unique identifier. It is used to create a unique key.
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