Generate Random String - CSharp System

CSharp examples for System:Random

Description

Generate Random String

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from ww w  .  j a  va2s  . c  om*/

public class Main{
        public static string GenerateRandomString(int length)
        {
            const string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var random = new Random(DateTime.Now.Millisecond);
            var output = string.Empty;
            for(var i = 0; i < length; i ++)
            {
                output += characters[random.Next(characters.Length - 1)];
            }

            return output;
        }
}

Related Tutorials