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 System.Collections;
/// <summary>
/// Summary description for RandomCls
/// </summary>
public class RandomCls : Random
{
private ArrayList element = null;
private int min;
private int max;
public RandomCls(int min, int max)
{
this.min = min;
this.max = max;
element = new ArrayList();
}
public void add(int index)
{
if (!element.Contains(index))
element.Add(index);
}
public object this[int index]
{
get { return element[index]; }
set { element[index] = value; }
}
public override int Next()
{
return base.Next(this.min, this.max);
}
public int getDifferenceRandomNumber()
{
int number ;
do
number = this.Next(min, max);
while (element.Contains(number) && element.Count < max - 1);
add(number);
return number;
}
public int[] getArrayRandom()
{
int[] temp = new int[max - min];
for (int i = 0; i < max - min; i++)
{
temp[i] = getDifferenceRandomNumber();
}
return temp;
}
}
调用:
int[] array = new int[100];
RandomCls randomUsr = new RandomCls(1, 100);
array = randomUsr.getArrayRandom();
本文介绍了一个自定义的随机数生成类RandomCls,该类能够生成指定范围内的随机数,并且可以确保生成的随机数不重复。通过使用ArrayList来存储已生成的随机数,实现了在指定范围内获取不重复随机数的功能。
494

被折叠的 条评论
为什么被折叠?



