perl-5.38.0
srand EXPR
srand

Sets and returns the random number seed for the rand operator.

rand 演算子のためのシード値を設定して返します。

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 (see below). 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:

この関数のポイントは、プログラムを実行するごとに rand 関数が 異なる乱数列を生成できるように rand 関数の「種」を 設定することです。 srand を引数付きで呼び出すと、これを種として使います; さもなければ(だいたい)ランダムに種を選びます(後述)。 どちらの場合でも、Perl 5.14 からは種を返します。 特定の時期の Perl でのみ 動作することを知らせるには以下のようにします:

    use v5.14;  # 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).

srand が明示的に呼び出されなかった場合、最初に rand 演算子を使った時点で暗黙に引数なしで呼び出されます。 しかし、最近の Perl でプログラムが srand を 呼び出したいであろう状況がいくつかあります。 一つはテストやデバッグのために予測可能な結果を生成するためです。 この場合、srand($seed) ($seed は毎回同じ値を使う) を使います。 もう一つの場合としては、子プロセスが親や他の子プロセスと同じ種の値を 共有することを避けるために、fork の後に srand を 呼び出したいかもしれません。

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.

srand (引数なし)をプロセス中で複数回 呼び出しては いけません。 乱数生成器の内部状態はどのような種によって提供されるものよりも 高いエントロピーを持っているので、srand() を再び呼び出すと ランダム性が 失われます

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.

srand のほとんどの実装では整数を取り、小数を暗黙に 切り捨てます。 これは、srand(42) は普通 srand(42.1) と同じ結果になることを意味します。 安全のために、srand には常に整数を渡しましょう。

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.

返された種の典型的な利用法は、実行毎のテストを利用可能な時間内に完全に 行うには組み合わせが多すぎるテストプログラム用です。 毎回ランダムなサブセットをテストし、もし失敗したら、その実行で使った 種をログに出力することで、後で同じ結果を再現するために使えます。

If the PERL_RAND_SEED environment variable is set to a non-negative integer during process startup then calls to srand() with no arguments will initialize the perl random number generator with a consistent seed each time it is called, whether called explicitly with no arguments or implicitly via use of rand(). The exact seeding that a given PERL_RAND_SEED will produce is deliberately unspecified, but using different values for PERL_RAND_SEED should produce different results. This is intended for debugging and performance analysis and is only guaranteed to produce consistent results between invocations of the same perl executable running the same code when all other factors are equal. The environment variable is read only once during process startup, and changing it during the program flow will not affect the currently running process. See perlrun for more details.

If the PERL_RAND_SEED environment variable is set to a non-negative integer during process startup then calls to srand() with no arguments will initialize the perl random number generator with a consistent seed each time it is called, whether called explicitly with no arguments or implicitly via use of rand(). The exact seeding that a given PERL_RAND_SEED will produce is deliberately unspecified, but using different values for PERL_RAND_SEED should produce different results. This is intended for debugging and performance analysis and is only guaranteed to produce consistent results between invocations of the same perl executable running the same code when all other factors are equal. The environment variable is read only once during process startup, and changing it during the program flow will not affect the currently running process. See perlrun for more details. (TBT)

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.

rand は暗号学的に安全ではありません。 セキュリティ的に重要な状況でこれに頼るべきではありません。 これを書いている時点で、いくつかのサードパーティ CPAN モジュールが 作者によって暗号学的に安全であることを目的とした乱数生成器を 提供しています: Data::Entropy, Crypt::Random, Math::Random::Secure, Math::TrulyRandom などです。