C# Unique Number

Simple method for generating a unique numeric string in C#, logged here for future reference. The source is pretty self explanatory, on each itieration test the length of the random number, generate a new single character number and append to the random number until we have the length as defined by randomNumberLength.

private static string GetRandomNumber(int randomNumberLength)
{
    System.Random rnd = new Random();

    string randomValue = string.Empty;

    while (randomValue.ToString().Length < randomNumberLength)
    {
        // Generate 1 character random number
        long nextRandomNumber = rnd.Next(0, 9);

        // Append the next random number
        randomValue = randomValue + nextRandomNumber.ToString();
    }

    return randomValue;
}
Advertisements
This entry was posted in .NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s