Create Random Code - CSharp System

CSharp examples for System:Random

Description

Create Random Code

Demo Code


using System.Web;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;
using System;//from w  w  w. j  a  va  2  s . co m

public class Main{
        public static string CreateRandomCode(int length)
        {
            int rand;
            char code;
            string randomcode = String.Empty;
            System.Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                rand = random.Next();
                if (rand % 3 == 0)
                {
                    code = (char)('A' + (char)(rand % 26));
                }
                else
                {
                    code = (char)('0' + (char)(rand % 10));
                }
                randomcode += code.ToString();
            }
            return randomcode;
        }
}

Related Tutorials