产生随机序列没有重复序列

2023-09-11 05:23:28 作者:溺于你的心海

我看了几个帖子在这里对产生随机序列不重复(例如http://stackoverflow.com/questions/693880/create-random-number-sequence-with-no-repeats)并决定实现它为我自己的需要

I read a couple of posts here about generating random sequence without repeats (for example http://stackoverflow.com/questions/693880/create-random-number-sequence-with-no-repeats) and decided to implement it for my own need

实际上它是施加与当前计数器的位一些非破坏性(可逆)操作,以获得应该只出现一次,一个伪随机数的算法。由于操作是可逆的,不同来源的数字会给出不同的结果数量。

Actually it was an algorithm applying some non-destructive (reversible) operations with the bits of the current counter in order to get a pseudo-random number that should appear only once. Since the operations are reversible, different source numbers will give different result numbers.

有至少数操作成为可能,如Exchange两位,颠倒了一下,循环移位。如果我们用唯一提到的,序列的质量也不会很大,因为在附近的计数器将产生具有零和一的类似数目的结果。真正的游戏规则改变者是XOR一位被另一位。现在序列看起来好多了,但问题是:

There are at least several operation possible like exchange two bits, invert a bit, cyclic shift. If we use only ones mentioned, the quality of the sequence won't be great since the nearby counter will produce results with similar number of zeros and ones. The real game changer was xor one bit by another bit. Now the sequences looks much better, but the questions are:

是否有将有足够的操作的最小的子集(例如翻转位+ XOR位被另一位),并添加任何其他的只是会使算法难以同时给没有额外的好处,读 如何大约猜出为了给定范围内的操作数的顺序是不够好。例如200的操作对数字从0到31提供了视觉效果不错,但200的操作的范围0..199有时会关闭数块。 是否有一个算法或测试套件来测试这些序列。我知道和使用一次的套房,可以测试一般随机序列,但是这一次是不同的,因此可能是一些特殊的套装需要或至少一些转换回一般的随机世界

更新:由于我张贴在这里评论,还有已经像这样一台发电机:AES加密,但不幸的是它只能用于128位的范围

UPDATE: As I posted in the comment here, there's already a generator like this: AES Encryption, but unfortunately it can only be used for 128-bit ranges.

感谢

最大

推荐答案

问题:

生成的1和N之间的唯一的随机整数的列表。

Generate a list of unique random integers between 1 and N.

解决方法:

生成N个随机数;高斯或均匀... 在对它们进行排序;保存的索引(即每个值的列表中的位置) 的排序的指数列表中。

在Matlab中:

z = rand( [N 1] );
[dummy iz] = sort(z);

%IZ是你的清单。

% iz is your list.