Perl兰特的论点有多大? [英] How big can the argument to Perl's rand be?

查看:82
本文介绍了Perl兰特的论点有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rand(n)返回0n之间的数字. rand对于随机性",对于我的平台上达到整数限制的所有参数,是否都能按预期工作?

rand(n) returns a number between 0 and n. Will rand work as expected, with regard to "randomness", for all arguments up to the integer limit on my platform?

推荐答案

这将取决于您的 randbits 值:

This is going to depend on your randbits value:

rand调用系统的随机数生成器(或其中一个 编译到您的Perl副本中).在此讨论中,我将其称为 生成器RAND使其与Perl的函数rand区别开来.兰德生产 从0到2的整数** randbits-1(含1和2),其中randbits是很小的 整数.要查看您的perl中的内容,请使用命令'perl -V:randbits'.常见值为15、16或31.

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.

当您使用参数arg调用rand时,perl将该值作为 整数并计算该值.

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

但是随着arg与2 ** randbits相比变大,事情变得 有问题的.假设有一个机器,其中randbits = 15,所以RAND范围 从0..32767.也就是说,每当我们致电RAND时,我们都会得到32768之一 可能的值.因此,当我们调用rand(arg)时,我们得到32768之一 可能的值.

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兰特的论点有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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