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

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

问题描述

我正忙着在C#与编码的数组。我可以用随机生成填满它,但现在是我的问题我如何做到这一点,但这样我可以检查是否值已经是数组中,如果是这样产生一个新的价值

额外的信息:结果
最高值:100结果
元素的数量:100

重要PLZ工作的进一步ON MY IDEA

我的想法

 公共无效FillArray(INT [] A,INT范围)
{
    的for(int i = 0; I<则为a.length; i ++)
    {
        A [I] = ValidNumber(T,I,范围);
    }
} / *填充数组* /

选择排序的实施

 公共无效选择排序(INT [] A)
{
    INT K,X;
    的for(int i = 0; I<则为a.length - 1; i ++)
    {
        K = 1;
        X = A [K];
        对于(INT J = I + 1; J<则为a.length; J ++)
        {
            如果(A [J]< X)
            {
                K =焦耳;
                X = A [K];
            }
        }
        A [K] = A [I]
        A [I] = X;
    }
} / *选择排序* /

这些都只是一些想法现在我想知道我可以修复它​​,所以我可以看看有选择排序,如果有ALLREAD那里(fillarray)是相同的,如果让一个新的随机值替换它。所以,我想创建一个整数随机序列 - 以随机的顺序,以100 1


解决方案

  

我如何做到这一点,但这样我可以检查是否值已经是数组中,如果是这样产生一个新的价值


您不这样做,直到永远,因为的这是一个非常糟糕的主意的。

要说明为什么这是一个可怕的想法,考虑同样的问题的另一个版本:通过下列方法一万个号码为一个随机进行排序:


  1. 选择从一个数百万。

  2. 检查,看它是否在名单上了。

  3. 如果是,回到步骤1

  4. 否则,添加的数量到列表中。

  5. 是否有名单上百万的项目?如果是的话,你就大功告成了。如果没有,回到步骤1。

显然,这工作。这是个好主意吗?让我们假设你几乎完成。名单上有999999项。唯一缺少的产品857313.你会怎么做?你选择一个随机数,也就是说,12.现在你检查清单上的项目999999,看看其中是否是12。12可能是您选择的第一个号码,所以它可能是快速找到它。或者它可能是最后的一个,因此将需要很长的时间。平均而言,它会采取500000检查是否12就行了。它是,因为仅存在一个从列表丢失数目

12没有发挥出来。返回到开始处。另一种选择随机数,说53259.是不是就行了?另外五十万检查。

请这样做,直到你产生857313,这恰好每隔一个百万尝试。

因此​​,平均而言,摆在列表中的最后一个项目需要50万点¯x百万=五线一百个十亿比较。这可能需要更多的方式。这可能需要数万亿的比较。或者,你可能会得到幸运,它需要一个。但平均而言,5000亿的比较。

这是一个的可怕的方式来产生一个列表的随机排序。

有两种好办法,使列表的随机排序。

(1)使能给出排序排序功能的列表中选择设备。提供的稳定的是基于随机种子排序

请注意,您应该的的通过当被问及返回随机结果的方法产生一个随机排序比B A大?这是一种不稳定的排序;许多排序算法是pdicated在一个稳定的排序顺序$ P $,并给予一个不稳定的排序顺序时,将进入无限循环或有其他不良行为。

这算法为O(n LG N),并具有很好的特性,这是很容易写出来的标准件,因为其他的答案表示。这也是典型的小实现列表速度极快。

(2)从随机源列表中选择通过索引项,的删除的它,当您去源列表,并把它的目标名单上。

,后者被称为Knuth的随机或费 - 耶茨洗牌,并且它是一个非常快的算法。你可以做到位了,变异现有数组打乱顺序,或创建一个新的列表。它也有很好的特性,你可以通过支付戏,洗牌的名单顶,你需要它。如果你有一百万的项目洗牌,但你只需要前一百名,你可以锻炼身体的排序顺序百强之首,并呼吁它好。

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

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

IMPORTANT PLZ WORK FURTHER ON MY IDEA

my idea

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

Implementation of selection sort

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 */

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. Choose a number from one to a million.
  2. Check to see if it is on the list already.
  3. If it is, go back to step 1
  4. Otherwise, add the number to the list.
  5. Does the list have one million items on it? If yes, you're done. If not, go back to step 1.

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 didn't work out. Go back to the beginning. Choose another random number, say, 53259. Is that on the list? Another half-million checks.

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

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.

There are two good ways to make a random ordering of a list.

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

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.

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) 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.

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天全站免登陆