rand(n)
返回 0
和 n
之间的数字。对于我的平台上达到整数限制的所有参数,rand
是否会按照预期的“随机性”工作?
最佳答案
这将取决于您的 randbits
值:
rand calls your system's random number generator (or whichever one was compiled into your copy of Perl). For this discussion, I'll call that generator RAND to distinguish it from rand, Perl's function. RAND produces an integer from 0 to 2**randbits - 1, inclusive, where randbits is a small integer. To see what it is in your perl, use the command 'perl -V:randbits'. Common values are 15, 16, or 31.
When you call rand with an argument arg, perl takes that value as an integer and calculates this value.
arg * RAND rand(arg) = --------------- 2**randbits
This value will always fall in the range required.
0 <= rand(arg) < arg
But as arg becomes large in comparison to 2**randbits, things become problematic. Let's imagine a machine where randbits = 15, so RAND ranges from 0..32767. That is, whenever we call RAND, we get one of 32768 possible values. Therefore, when we call rand(arg), we get one of 32768 possible values.
关于perl - Perl 的 rand 参数可以有多大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5993439/