11

Jun

2008

Generating Random Number and String in C#

The following code returns a random number between min and max:

private int RandomNumber(int min, int max)
{
    Random random = new Random();
    return random.Next(min, max);
}

At some point, you may also want to generate random strings.

/// 
/// Generates a random string with the given length
/// 
/// Size of the string
/// If true, generate lowercase string
/// Random string
private string RandomString(int size, bool lowerCase)
{
    StringBuilder builder = new StringBuilder();
    Random random = new Random();
    char ch ;
for(int i=0; i

Comments are closed.