五独特,随机数从一个子集 [英] Five unique, random numbers from a subset

查看:121
本文介绍了五独特,随机数从一个子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似的问题上来了很多,有可能是没有明确的答案,但是我想从数字的子集,它是潜在的无限(也许0-20,或0-1,000,000)产生五个唯一随机数。
唯一美中不足的是,我不希望有执行,而循环或填充数组。

I know similar questions come up a lot and there's probably no definitive answer, but I want to generate five unique random numbers from a subset of numbers that is potentially infinite (maybe 0-20, or 0-1,000,000).
The only catch is that I don't want to have to run while loops or fill an array.

我的当前方法是简单地产生5随机数从一个子集减去最后五个数字。如果任何号码彼此匹配,那么他们去到它们各自的位置,在该子集的末端。因此,如果在第四数目的任何其他号码相匹配,这将下注设置为从最后数的第4位。

My current method is to simply generate five random numbers from a subset minus the last five numbers. If any of the numbers match each other, then they go to their respective place at the end of the subset. So if the fourth number matches any other number, it will bet set to the 4th from the last number.

有没有人有一个方法,那就是随机不够,不涉及昂贵的循环或数组?

Does anyone have a method that is "random enough" and doesn't involve costly loops or arrays?

请记住这样的好奇心,而不是一些关键问题。我想AP preciate,如果每个人都没有张贴你为什么有这个问题?答案。我只是在寻找的想法。
非常感谢!

Please keep in mind this a curiosity, not some mission-critical problem. I would appreciate it if everyone didn't post "why are you having this problem?" answers. I am just looking for ideas.
Thanks a lot!

推荐答案

一个随机数呼叫就足够了。

One random number call is enough.

如果你想选择的5种独特的编号在范围内1-N的一个子集,然后在1选择一个随机数(N选择R)。

If you want to choose a subset of 5 unique numbers in range 1-n, then select a random number in 1 to (n choose r).

请一个一对一映射,从1到(n选择R),以一组可能的5要素的子集,和你做。这个映射是标准的,可以在网络上找到,比如这里:<一href="http://msdn.microsoft.com/en-us/library/aa289166%28VS.71%29.aspx">http://msdn.microsoft.com/en-us/library/aa289166%28VS.71%29.aspx

Keep a 1-1 mapping from 1 to (n choose r) to the set of possible 5 element subsets, and you are done. This mapping is standard and can be found on the web, for instance here: http://msdn.microsoft.com/en-us/library/aa289166%28VS.71%29.aspx

作为一个例子:

这五个数字考虑产生两个数的子集的问题:

Consider the problem of generating a subset of two numbers from five numbers:

的可能2元件子集{1,...,5}是

The possible 2 element subset of {1,..., 5} are

1. {1,2}
2. {1,3}
3. {1,4}
4. {1,5}

5. {2,3}
6. {2,4}
7. {2,5}

8. {3,4}
9. {3,5}

10. {4,5}

现在5选2为10。

所以我们选择从1到10,说我们得到8。现在我们生成序列在上面的第8个元素的随机数:这给{3,4},让您随心所欲的两个数字是3和4

So we select a random number from 1 to 10. Say we got 8. Now we generate the 8th element in the sequence above: which gives {3,4}, so the two numbers you want are 3 and 4.

在MSDN网页我联系,告诉您一个方法来生成集,给出的数字。即给定8,它给回集合{3,4}。

The msdn page I linked to, shows you a method to generate the set, given the number. i.e. given 8, it gives back the set {3,4}.

这篇关于五独特,随机数从一个子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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