perl - Perl 的 rand 参数可以有多大?

标签 perl random

rand(n) 返回 0n 之间的数字。对于我的平台上达到整数限制的所有参数,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/

相关文章:

linux - 如果使用相同 IP/端口的连接断开,则 Shell 脚本警报

perl - Perl中的双索引,为什么会这样?

Perl Getopt 多次使用相同的选项

java - 如何在数组中生成随机 double ?

具有高效增删改查的Java数据结构

perl - 如何修复此 Perl 代码,使 1.1 + 2.2 == 3.3?

linux - 在 Linux 上用 perl fork 一个新进程

Java随机种子

Java - 生成随机数字簇

MySQL简单行数增量计数器