如何避免重复随机数,我想在asp.net中生成数十亿的随机数,但它重复

时间:2017-12-12 11:09:46

标签: c#

static Random r = new Random();

public static int Randfunc(int start, int end)
{
    List<int> numbers = new List<int>();
    int n = r.Next(start, end);
    return n;
}

1 个答案:

答案 0 :(得分:-1)

尝试类似的东西;

        var start = 0;//Min range of numbers
        var end = 1000;//Max range of numbers
        int i = 0;
        Random random = new Random();
        var numbers = new HashSet<int>();
        while(i < 100)//Collect 100 unique numbers
        {
            var number = random.Next(start, end);
            if (!numbers.Contains(number))
            {
                numbers.Add(number);
                i++;
            }
        }