Click here to Skip to main content
15,886,513 members
Articles / General Programming / Tools

C# Random String Generator

Rate me:
Please Sign up or sign in to vote.
4.90/5 (43 votes)
18 Apr 2013CPOL3 min read 204.5K   8.3K   89  
A C# class that can generate random strings and supports customisation.
using System;

namespace RandomStringGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Fixed size");
            RandomStringGenerator RSG = new RandomStringGenerator();
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate(25));

            Console.WriteLine("Variable size");
            RSG = new RandomStringGenerator();
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate(15,25));

            Console.WriteLine("Using pattern");
            RSG = new RandomStringGenerator();
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate("LLln*ssssL"));

            Console.WriteLine("Using only letters and numbers");
            RSG = new RandomStringGenerator();
            // Or we can use the constructor
            RSG.UseSpecialCharacters = false;
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate(30));

            Console.WriteLine("Using only special characters and numbers");
            RSG = new RandomStringGenerator();
            // Or we can use the constructor
            RSG.UseUpperCaseCharacters = false;
            RSG.UseLowerCaseCharacters = false;
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate(30));

            Console.WriteLine("Using your own alphabet");
            RSG = new RandomStringGenerator(false,true,true,false);
            // Or we can use the constructor
            RSG.LowerCaseCharacters = "абвгдежзиклмнопрстуфхчшщюяьъ".ToCharArray();
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate(30));

            Console.WriteLine("Using special limits");
            RSG = new RandomStringGenerator();
            RSG.MinLowerCaseCharacters = 2;
            RSG.MinSpecialCharacters = 20;
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate(35));

            Console.WriteLine("Using each character only once");
            RSG = new RandomStringGenerator();
            RSG.MinLowerCaseCharacters = 25;
            RSG.RepeatCharacters = false;
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate(25));

            Console.WriteLine("Using custom alphabet, pattern and unique chars");
            RSG = new RandomStringGenerator();
            RSG.LowerCaseCharacters = "абвгдежзиклмнопрстуфхчшщюяьъ".ToCharArray();
            RSG.RepeatCharacters = false;
            for (int i = 0; i < 5; i++)
                Console.WriteLine(RSG.Generate("lllnlllnlllsssL"));

            Console.WriteLine("Unique strings test");
            RSG = new RandomStringGenerator(false,false,true,false); // numbers only
            RSG.UniqueStrings = true;
            for (int i = 0; i < 9; i++)
                Console.WriteLine(RSG.Generate(1));

            Console.ReadKey(); // stop
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
France France
I'm currently a master student at the University of Maine in France. I'm studying computer science and in my spare time I enjoy programming using such languages as C,C++,C#,Java and Ruby.

Comments and Discussions