功能 srand() Perl

在Perl编程语言都有一个内置的功能 srand().

这个函数是用于设置或获得的数量 序列的数值从一个功能返回 rand().

大多数情况下,功能的 srand() 中所用的测试,以每 运行测试功能 rand() 返回相同的价值。

这里是一个例子码:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use feature qw(say);

srand(16);

say int(rand(10));
say int(rand(10));
say int(rand(10));
say int(rand(10));
say int(rand(10));

结果该程序的总是相同的5个数字:

1
0
9
3
3

该程序使用 int(rand(10)) ,以获得随机数之间的 0(含) 到 9(含)。

在代码中有一个叫 srand(16);. 这一呼吁设置功能 rand() 应该回 数以伪随机序列号 16. 因此,每一个运行这个程序导致 相同的结果。 如果这节目不是叫功能 srand(),然后 不同的运行程序的可能返回不同的价值观。

参数

功能 srand() 可以不经任何辩论或得到她的完全一论点。

在这种情况下,如果你通过了多个参数,它将错误:

Too many arguments for srand at script.pl line 3, near "2)"
Execution of script.pl aborted due to compilation errors.

功能不使用 $_. 如果有需要使用它,则必须明确地传递给函 srand($_);.

功能期望一个数字作为参数值。 如果功能接收不是一个数字 她导致他的号码。

功能丢弃的小数部分和的迹象。 srand(-2.9) 相同 srand(2). 这里是一个例代码表示:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

foreach my $n (-2, 2, 2.1, 2.9, -2.1, -2.9) {
    my $real_n = srand($n);
    my @arr;
    foreach (1..5) {
        push @arr, int(rand(10));
    }
    printf "%4s  %s  (%s)\n", $n, $real_n, join(', ', @arr);
}

回值

开始Perl5.14一个功能 srand() 总是返回的一个数 14294967296,或一串 0 but true. 这个数字是数量序列中使用的功能 rand().

如果功能 srand() 呼吁没有参数,它返回的数量 顺序安装的自动。

如果功能电话与一个参数,它将返回值的序列号 安装这种说法:

▶ Run
#!/usr/bin/perl

use feature qw(say);

say srand(1573); # 1573
say srand(-2.9); # 2

有趣的是,序列号 0 职能返回串 0 but true:

▶ Run
#!/usr/bin/perl

print srand(0);

前Perl5.14,功能 srand() 总是返回的数量 1.

官方文件

这里是输出的命令 perldoc -f srand:

    srand EXPR
    srand   Sets and returns the random number seed for the "rand" operator.

            The point of the function is to "seed" the "rand" function so
            that "rand" can produce a different sequence each time you run
            your program. When called with a parameter, "srand" uses that
            for the seed; otherwise it (semi-)randomly chooses a seed. In
            either case, starting with Perl 5.14, it returns the seed. To
            signal that your code will work *only* on Perls of a recent
            vintage:

                use 5.014;  # so srand returns the seed

            If "srand" is not called explicitly, it is called implicitly
            without a parameter at the first use of the "rand" operator.
            However, there are a few situations where programs are likely to
            want to call "srand". One is for generating predictable results,
            generally for testing or debugging. There, you use
            "srand($seed)", with the same $seed each time. Another case is
            that you may want to call "srand" after a "fork" to avoid child
            processes sharing the same seed value as the parent (and
            consequently each other).

            Do not call "srand()" (i.e., without an argument) more than once
            per process. The internal state of the random number generator
            should contain more entropy than can be provided by any seed, so
            calling "srand" again actually *loses* randomness.

            Most implementations of "srand" take an integer and will
            silently truncate decimal numbers. This means "srand(42)" will
            usually produce the same results as "srand(42.1)". To be safe,
            always pass "srand" an integer.

            A typical use of the returned seed is for a test program which
            has too many combinations to test comprehensively in the time
            available to it each run. It can test a random subset each time,
            and should there be a failure, log the seed used for that run so
            that it can later be used to reproduce the same results.

            "rand" is not cryptographically secure. You should not rely on
            it in security-sensitive situations. As of this writing, a
            number of third-party CPAN modules offer random number
            generators intended by their authors to be cryptographically
            secure, including: Data::Entropy, Crypt::Random,
            Math::Random::Secure, and Math::TrulyRandom.

相关的主题

其他文章