Friday, September 25, 2009

Generate Random Numbers and Strings

//generate random number between min and max
private int RandomNumber(int min, int max)
{
  Random random = new Random();
  return random.Next(min, max);
}

//generate random string
private string RandomString(int size, bool lowerCase)
{
  StringBuilder builder = new StringBuilder();
  Random random = new Random();
  char ch ;
  for(int i=0; i&$60;size; i++)
  {
    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
    builder.Append(ch);
  }
  if(lowerCase)
  return builder.ToString().ToLower();
  return builder.ToString();
}

JavaScript random (needed for on load / refresh updates)

var pos = Math.floor((5 - 0) * Math.random()) + 1; //NUM BETWEEN 1 & 5
if (pos == 1) {
fade(headerNum);
}
else if (pos == 2) {
SlideToLeft(headerNum);
}
else if (pos == 3) {
SlideDown(headerNum);
}
else if (pos == 4) {
SlideUp(headerNum);
}
else {
SlideToRight(headerNum);
}


Source:
http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx

No comments:

Post a Comment