Gets a string from the English alphabet at random - CSharp System

CSharp examples for System:Random

Description

Gets a string from the English alphabet at random

Demo Code


using System.Text;
using System.Security.Cryptography;
using System;/*  w w  w . ja  va2  s  .c  o m*/

public class Main{
        /// <summary>
        /// Gets a string from the English alphabet at random
        /// </summary>
        public static string GenerateRandomAlphabetString(int length)
        {
            string allowedChars = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var rnd = SeedRandom();

            char[] chars = new char[length];
            for (int i = 0; i < length; i++)
            {
                chars[i] = allowedChars[rnd.Next(allowedChars.Length)];
            }

            return new string(chars);
        }
        private static Random SeedRandom()
        {
            return new Random(Guid.NewGuid().GetHashCode());
        }
}

Related Tutorials