算法生成一个随机数字串,万字符的长度?随机数、算法、字串、长度

2023-09-11 06:48:51 作者:忘记过往

可以是任何语言,甚至伪code。有人问我这个在接受采访时的问题,并很好奇什么你们能拿出。

Can be in any language or even pseudocode. I was asked this in an interview question, and was curious what you guys can come up with.

推荐答案

我觉得这是一个有趣的问题 - 的发生位数使用标准库函数的明显的答案几乎肯定是有缺陷的,如果你想生成每一个可能的10000位以相同的概率数...

I think this is a trick question - the obvious answer of generating digits using a standard library routine is almost certainly flawed, if you want to generate every possible 10000 digit number with equal probability...

如果一个算法随机数生成器维护的 N 的状态位,那么显然它可以生成的最多 2 N 可能不同的输出序列,因为只有2 N 不同的初始配置。

If an algorithmic random number generator maintains n bits of state, then clearly it can generate at most 2n possible different output sequences, because there are only 2n different initial configurations.

2 33219 < 10 10000 < 2 33220 ,所以如果你的算法使用小于33220位的内部状态,它的不可能的产生有些10 10000 可能的10000位数字(十进制)的数字。

233219 < 1010000 < 233220, so if your algorithm uses less than 33220 bits of internal state, it cannot possibly generate some of the 1010000 possible 10000-digit (decimal) numbers.

典型的标准库中随机数发生器不会使用这样多的内部状态的任何事情。即使是梅森难题(中最经常提到的生成与我所知道的国家大型一类),只保留624 32位状态字(= 19968位)。

Typical standard library random number generators won't use anything like this much internal state. Even the Mersenne Twister (the most frequently mentioned generator with a large state that I'm aware of) only keeps 624 32-bit words (= 19968 bits) of state.