Generate Unique Random String

  • Thread starter Thread starter Juan
  • Start date Start date
Hi Juan,
one way you could do this is shown below. The code creates a string of
pre-determined length that consists only of the character A-Z:


int stringLength = 10;

Random rnd = new Random((int)System.DateTime.Now.Ticks);
System.Text.StringBuilder randomText = new
System.Text.StringBuilder(stringLength);

//value of A in ascii codes
int minValue = 65;

//value of Z in ascii codes
int maxValue = 90;

//the range that we are allowed to go above the min value
int randomRange = maxValue - minValue;

double rndValue;

for(int i=0; i<stringLength; i++)
{
rndValue = rnd.NextDouble();

randomText.Append((char)(minValue + rndValue * randomRange));
}

Console.WriteLine(randomText.ToString());


Hope that helps
Mark R Dawson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top