Random number generation in ASP.NET

ఏదో ఒక్క నెంబర్ జనరేషన్ in ASP.NET

Step 1:

write a class file.
eg:

Create a cs file named as rand.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.VisualBasic;

/// <summary>
/// Summary description for random_number
/// </summary>
public class random_number
{
public string GetRandomNumber(int length, string characterSet)
{

string randomData = “”;

int position = 0;

byte[] data = new byte[length];

int characterSetLength = characterSet.Length;

System.Security.Cryptography.RandomNumberGenerator random = System.Security.Cryptography.RandomNumberGenerator.Create();

random.GetBytes(data);

for (int index = 0; (index < length); index++)
{

position = data[index];

position = (position % characterSetLength);

randomData = (randomData + characterSet.Substring(position, 1));

}

return randomData;

}

}

step 2:

use this in codebehind of any aspx page

random_number r1 = new random_number();
string str= r1.GetRandomNumber(5, “ABCDEFGHIJLLMNOPQRSTUVWXYZ_1234567890”);

Advertisements