How to generate a alpha a - z randomly

  • Thread starter Thread starter ad
  • Start date Start date
ad said:
Hi,
How can a generate an alpha a - z randomly?
Try:

static void Main(string[] args)
{
Random r = new Random();
//ASCII a = 97, z = 122
char c = Convert.ToChar(r.Next(97, 122));
Console.Out.WriteLine(c);
}
 
Hi,

Random rnd = new Random();

for (int i = 0; i < 10; i++)
{
char c = (char) rnd.Next('a', 'z' + 1);
Console.WriteLine(c);
}
 
Dave said:
ad said:
Hi,
How can a generate an alpha a - z randomly?

Try:

static void Main(string[] args)
{
Random r = new Random();
//ASCII a = 97, z = 122
char c = Convert.ToChar(r.Next(97, 122));
Console.Out.WriteLine(c);
}

That should be:

char c = Convert.ToChar(r.Next(97, 123));

Parameters
----------
minValue
The inclusive lower bound of the random number returned.

maxValue
The exclusive upper bound of the random number returned. maxValue must
be greater than or equal to minValue.
 
Hi,

You'll find that 'z' will never be returned, however, since the second
argument to the Random.Next method is exclusive.

--
Dave Sexton

Dave Shooter said:
ad said:
Hi,
How can a generate an alpha a - z randomly?
Try:

static void Main(string[] args)
{
Random r = new Random();
//ASCII a = 97, z = 122
char c = Convert.ToChar(r.Next(97, 122));
Console.Out.WriteLine(c);
}
 
the Random class is a .NET framework class and therefore is accessible
by both VB.NET and C#
 
Hi,

I'm not sure what you mean, but this is a C# newsgroup and the code that I
posted was written in C#, and, it works.
 

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