Generates a random string with the given length - CSharp System

CSharp examples for System:Random

Description

Generates a random string with the given length

Demo Code


using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System.Reflection;
using System.IO;/* w ww .  jav  a 2 s  .c  o m*/
using System.Globalization;
using System.Data.Entity.ModelConfiguration.Design.PluralizationServices;
using System;

public class Main{
        #endregion ReplaceAccents

        #region GetRandomString

        /// <summary>
        /// Generates a random string with the given length
        /// intRandomHelper is to prevent two same Random result
        /// </summary>
        public static string GetRandomString(int intSize)
        {
            StringBuilder strBuilder = new StringBuilder();
            Random rndObject = new Random();
            for (int IndCar = 0; IndCar < intSize; IndCar++)
            {
                int intRandom = Convert.ToInt32(Math.Floor(26 * rndObject.NextDouble() + 65));
                char carRandom = Convert.ToChar(intRandom);
                strBuilder.Append(carRandom);
            }
            rndObject = null;
            return strBuilder.ToString().ToLower();
        }
}

Related Tutorials