搜档网
当前位置:搜档网 › C#生成随机数或随即字母

C#生成随机数或随即字母

public class Rand
{
///


/// 生成随机数字
///

/// 生成长度
///
public static string Number(int Length)
{
return Number(Length, false);
}

///
/// 生成随机数字
///

/// 生成长度
/// 是否要在生成前将当前线程阻止以避免重复
///
public static string Number(int Length,bool Sleep)
{
if(Sleep)
System.Threading.Thread.Sleep(3);
string result = "";
System.Random random = new Random();
for (int i = 0; i < Length; i++)
{
result += random.Next(10).ToString();
}
return result;
}

///
/// 生成随机字母与数字
///

/// 生成长度
///
public static string Str(int Length)
{
return Str(Length, false);
}
///
/// 生成随机字母与数字
///

/// 生成长度
/// 是否要在生成前将当前线程阻止以避免重复
///
public static string Str(int Length, bool Sleep)
{
if(Sleep)
System.Threading.Thread.Sleep(3);
char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0,n);
result += Pattern[rnd];
}
return result;
}


///
/// 生成随机纯字母随机数
///

/// 生成长度
///
public static string Str_char(int Length)
{
return Str_char(Length, false);
}

///
/// 生成随机纯字母随机数
///

/// 生成长度
/// 是否要在生成前将当前线程阻止以避免重复
///
public static string Str_char(int Length, bool Sleep)
{
if (Sleep) System.Threading.Thread.Sleep(3);
char[] Pattern =

new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
}









Dotnet.Frameword中提供了一个专门产生随机数的类System.Random,计算机并不能产生完全随机的数字,它生成的数字被称为伪随机数,它是以相同的概率从一组有限的数字中选取的,所选的数字并不具有完全的随机性,但就实用而言,其随机程度已经足够了。

在使用随机数时,要先初始化一个随机数发生器.有两种方法;

第一种方法不指定随机种子,系统自动选取当前时前作随机种子:

Random ra=new Random();

第二种方法是指定一个int型的参数作为随机种子:

Random ra=new Random(int iseed);

初始化完成后,用Random.Next()方法产生随机数。

ra.Next(); 它返回一个大于或等于零而小于2,147,483,647的数.

下面我们介绍它的重载函数和其它一些方法。



public virtual int Next(int);

用法:ra.next(20)

返回一个小于所指定最大值(此处为20)的正随机数。



public virtual int Next(int minValue, int maxValue);

用法:ra.next(1,20)

返回一个指定范围内(此处为1-20之间)的随机数.



类System.Random还有几个方法分别是:

公共方法:

NextBytes用随机数填充指定字节数组的元素。

NextDouble返回一个介于 0.0 和 1.0 之间的随机数。



受保护的方法:

Sample返回一个介于 0.0 和 1.0 之间的随机数,只允许子类对象访问。

要在一段数字区间内随机生成若干个互不相同的随机数,比如在从1到20间随机生成6个互不相同的整数。

可以参考下面两个函数:

differSamenessRandomNum与getRandomNum:



public int[] differSamenessRandomNum(int num,int minValue,int maxValue)

//在区间[minValue,maxValue]取出num个互不相同的随机数,返回数组。

{



Random ra=new Random(unchecked((int)DateTime.Now.Ticks));//保证产生的数字的随机性

int[] arrNum=new int[num];

int tmp=0;

for (int i=0;i<=num-1;i++)

{

tmp=ra.Next(minValue,maxValue); //随机取数

arrNum[i]=get RandomNum(arrNum,tmp,minValue,maxValue,ra); //取出值赋到数组中



}

return arrNum;

}

函数getNum是一递归,用它来检测生成的随机数是否有重复,如果取出来的数字和已取得的数字有重复就重新随机获取。

public int getRandomNum(int[] arrNum,int tmp,int minValue,int maxValue,Random

ra)

{

int n=0;

while (n<=arrNum.Length-1)

{

if (arrNum[n]==tmp) //利用循环判断是否有重复

{

tmp=ra.Next(minValue,maxValue); //重新随机获取。

getRandomNum(arrNum,tmp,minValue,maxValue,ra);

//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。

}

n++;

}

return tmp;

}


相关主题