Lewis's Space

顛覆...Please let me feel that you have visited here today..thks...

一返回指定范围的不重复随机数类

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();


阅读更多
个人分类: C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

一返回指定范围的不重复随机数类

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭