Posts

Showing posts from March, 2013

C# generate random string of different length

If you want to generate random strings of different different lengths, than use below function. Here size is an input variable for length of string.      public static string RandomString( int size)       {             string builder = string .Empty;              Random random = new Random ();              char ch;              for ( int i = 0; i < size; i++)              {                  ch = Convert .ToChar( Convert .ToInt32( Math .Floor(26 * random.NextDouble() + 65)));                  builder += ch.ToString();              }              return builder.ToString();          }

C# generate random numbers in a range

 If you want to generate a random nomber in a range than use below function. it has two inputs "min" -  enter min value of your range and "max" - enter max value of your range.  public static int RandomNumber( int min , int max )          {              Random random = new Random ();              return random.Next(min, max);          } Here in above function Random is a class. this is exist in .net framework by default. you should only use namespace "using System" for this.