Saturday, August 7, 2010

How to generate a random string

How to generate a random string in ASP.Net C#

 When i was searching on internet for the solution of a problem that how can i generate a random string  from the list of chosen characters of chosen

length i came to a solution which is given below,that is very simple ,neat and clean where you just enter the char-pool from which you want to generate

your random string and choose the length of string this will give the perfect answer for your problem.


public void GetRandomString()

    {

        Random rnd = new Random();

        int length = 6;

        string charPool

        = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

        StringBuilder rs = new StringBuilder();



        while (length-- > 0)

            rs.Append(charPool[(int)(rnd.NextDouble() * charPool.Length)]);



         lbl_mess.Text= rs.ToString();

    }

This help me great and hope you will also enjoy this!!!!!!!!!!!




No comments:

Post a Comment