C# 中的随机数生成器 - 唯一值 [英] Random number generator in C# - unique values

查看:84
本文介绍了C# 中的随机数生成器 - 唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于使用 C# 编写数组.我可以用随机生成器填充它,但现在我的问题是我该怎么做,但这样我就可以检查该值是否已经在数组中,如果是,则生成一个新值

I'm busy in C# with coding an array. I can fill it up with random generators but now is my question how do i do this but so that i can check if the value is already in the array and if so generate an new value

额外信息:
最大值:100
元素数:100

Extra info:
Max value : 100
Number of elements : 100

重要的请继续我的想法

我的想法

public void FillArray(int[] A, int Range)
{
    for (int I = 0; I < A.Length; I++)
    {
        A[I] = ValidNumber(T, I, Range);
    }
} /* Fill Array */

选择排序的实现

public void SelectionSort(int[] A)
{
    int K, X;
    for (int I = 0; I < A.Length - 1; I++)
    {
        K = I;
        X = A[K];
        for (int J = I + 1; J < A.Length; J++)
        {
            if (A[J] < X)
            {
                K = J;
                X = A[K];
            }
        }
        A[K] = A[I];
        A[I] = X;
    }
} /* Selection sort */

这些只是一些想法,现在我想知道如何修复它,以便我可以使用选择排序查看是否存在所有读取(填充数组),如果相同,则将其替换为新的随机值.所以我想用整数创建一个随机数组 - 从 1 到 100 以随机顺序

These are just some ideas now i want to know how i can fix it so i can look with selection sort if there is allread there (fillarray) that is same if so replace it with a new random value. And so i want to create a random array with ints - from 1 to 100 in a random order

推荐答案

我该怎么做,但这样我就可以检查该值是否已经在数组中,如果是,则生成一个新值

how do i do this but so that i can check if the value is already in the array and if so generate an new value

你永远不会那样做,因为那是一个非常糟糕的主意.

You don't do that, ever, because that is a very bad idea.

为了说明为什么这是一个糟糕的想法,请考虑同一问题的另一个版本:通过以下过程将一百万个数字按随机顺序排序:

To illustrate why it is a terrible idea, consider another version of the same problem: sort a million numbers into a random order by the following process:

  1. 选择一个从一到一百万的数字.
  2. 检查它是否已经在列表中.
  3. 如果是,请返回步骤 1
  4. 否则,将数字添加到列表中.
  5. 列表中有 100 万个项目吗?如果是,你就完成了.如果不是,请返回第 1 步.

显然这是有效的.这是个好主意吗?假设你快完成了.该列表中有 999999 项.唯一缺少的项目是 857313.你是做什么的?您选择一个随机数,例如 12.现在您检查列表中的 999999 个项目,看看它们中是否有任何一个是 12.12 可能是您选择的第一批数字之一,因此找到它可能会很快.或者它可能是最后一个,所以需要很长时间.平均需要 500000 次检查才能查看 12 是否在列表中.确实如此,因为列表中只缺少一个数字.

Clearly this works. Is it a good idea? Let's suppose you're almost done. The list has 999999 items on it. The only missing item is 857313. What do you do? You choose a random number, say, 12. Now you check the 999999 items on the list to see if any of them are 12. 12 might have been one of the first numbers you chose, so it might be fast to find it. Or it might be one of the last, so it will take a long time. On average it will take 500000 checks to see if 12 is on the list. And it is, since there is only one number missing from the list.

12 没有成功.回到起点.选择另一个随机数,例如 53259.它在列表中吗?另有 50 万张支票.

12 didn't work out. Go back to the beginning. Choose another random number, say, 53259. Is that on the list? Another half-million checks.

继续这样做,直到生成 857313,每百万次尝试就会发生一次.

Keep doing that until you generate 857313, which happens one every million tries.

因此,平均而言,将最后一项放入列表需要 500000 x 1000000 = 五千亿次比较.它可能需要更多.可能需要进行数万亿次比较.或者你可能很幸运,需要一个.但平均而言,有 5 万亿次比较.

So, on average, to put the last item in the list takes 500000 x 1000000 = five hundred billion comparisons. It might take way more. It might take several trillion comparisons. Or you might get lucky and it takes one. But on average, half a trillion comparisons.

这是一种可怕的方法来生成列表的随机排序.

This is a terrible way to produce a random ordering of a list.

对列表进行随机排序有两种好方法.

(1) 制作一个可以对给定排序函数的列表进行排序的设备.提供基于随机种子的稳定排序.

(1) Make a device which can sort a list given an ordering function. Provide a stable ordering that is based on a random seed.

请注意,当被问及A 是否大于 B?"时,您不应该通过创建一个返回随机结果的方法来生成随机排序.这是一个不稳定的排序;许多排序算法都基于稳定的排序顺序,并且在给定不稳定的排序顺序时会进入无限循环或有其他不良行为.

Note that you should not produce a random ordering by making a method that returns random results when asked "is A bigger than B?" That is an unstable ordering; many sort algorithms are predicated on a stable sort ordering and will go into infinite loops or have other bad behaviour when given an unstable sort ordering.

这个算法是 O(n lg n) 并且有一个很好的特性,它很容易写出标准部分,正如其他答案所表明的那样.对于典型实现中的小列表,它也非常快.

This algorithm is O(n lg n) and has the nice property that it is very easy to write out of standard parts, as other answers indicate. It is also extremely fast for small lists in typical implementations.

(2) 从源列表中随机选择索引项,将其从源列表中删除,并将其放入目标列表中.

(2) Choose an item by index from a source list at random, removing it from the source list as you go, and putting it on the destination list.

后者被称为 Knuth Shuffle 或 Fischer-Yates Shuffle,它是一种非常快的算法.您可以就地"执行此操作,将现有数组更改为无序排列,或创建新列表.它还有一个很好的特性,你可以付费玩",洗牌顶".根据您的需要选择列表.如果您有 100 万个项目要洗牌,而您只需要前 100 个项目,则可以计算出前 100 个项目的排序顺序并称其为好.

The latter is known as Knuth Shuffle or Fischer-Yates Shuffle, and it is a very fast algorithm. You can do it "in place", mutating an existing array into shuffled order, or by creating a new list. It also has the nice property that you can "pay for play", shuffling the "top" of the list as you need it. If you have a million items to shuffle but you only need the first one hundred, you can just work out the sort order for the first hundred and call it good.

这篇关于C# 中的随机数生成器 - 唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆