Random String - CSharp System

CSharp examples for System:Random

Description

Random String

Demo Code


using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.Collections.Generic;
using System;/* w w  w. ja  v a 2  s  .c  o m*/

public class Main{
        public static string RandomString(int len)
        {
            string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            char[] chars = new char[len];
            Random rd = new Random();

            for (int i = 0; i < len; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }

            return new string(chars);
        }
}

Related Tutorials