创建随机数序列中无重复 [英] Create Random Number Sequence with No Repeats

查看:207
本文介绍了创建随机数序列中无重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

独特的随机数为O(1)?

我想要一个伪随机数发生器,可以以随机的顺序生成没有重复数

I want an pseudo random number generator that can generate numbers with no repeats in a random order.

例如:

随机(10)

可能返回 5,9,1,4,2,8,3,7,6,10

might return 5, 9, 1, 4, 2, 8, 3, 7, 6, 10

有没有更好的方式来做到这一点除了使数字的范围和洗牌他们有关,或检查生成的列表重复?

Is there a better way to do it other than making the range of numbers and shuffling them about, or checking the generated list for repeats?


此外,我希望它是有效地产生大的数字,没有全部范围。

Also I want it to be efficient in generating big numbers without the entire range.


我看大家都建议洗牌算法。但是,如果我想生成大量的随机数(1024字节+),则该方法将采取很多更多的内存,如果我只是用一个普通RNG并插入到一组,直到它被指定长度,对不对?有没有更好的数学算法这一点。

I see everyone suggesting shuffle algorithms. But if I want to generate large random number (1024 byte+) then that method would take alot more memory than if I just used a regular RNG and inserted into a Set until it was a specified length, right? Is there no better mathematical algorithm for this.

推荐答案

您可能感兴趣的线性反馈移位寄存器。 我们用来建造这些了硬件,但我已经在软件中也做了他们。它采用了移位寄存器的一些异或运算,并反馈到输入位,如果你选择了正确的水龙头,你可以得到一个序列,只要这是作为寄存器的大小。也就是说,一个16比特LFSR可以产生一个序列65535长,没有重复。这是统计学随机的,但当然突出的重复。此外,如果它做错了,你可以得到一些令人尴尬的短序列。如果你查找LFSR,你会发现如何正确地构建他们的例子(这是说,最大长度)。

You may be interested in a linear feedback shift register. We used to build these out of hardware, but I've also done them in software. It uses a shift register with some of the bits xor'ed and fed back to the input, and if you pick just the right "taps" you can get a sequence that's as long as the register size. That is, a 16-bit lfsr can produce a sequence 65535 long with no repeats. It's statistically random but of course eminently repeatable. Also, if it's done wrong, you can get some embarrassingly short sequences. If you look up the lfsr, you will find examples of how to construct them properly (which is to say, "maximal length").

这篇关于创建随机数序列中无重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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