Thread-Rand-0.06 > Thread::Rand

名前

Thread::Rand - スレッド間で再現可能な乱数列

概要

  use Thread::Rand;             # rand()とsrand()をエクスポート

  use Thread::Rand ();          # sbusを完全修飾で呼ばなければならない

  BEGIN {Thread::Rand->global}  # グローバルにrand()とsrand()を置き換え

説明

                  *** A note of CAUTION ***

 This module only functions on Perl versions 5.8.0 and later.
 And then only when threads are enabled with -Dusethreads.  It
 is of no use with any version of Perl before 5.8.0 or without
 threads enabled.

                  *************************


                  *** 注   意   書   き ***

 このモジュールは、Perlのバージョン5.8.0以降で、かつ、-Dusethreads
 オプションでスレッドが有効になっている場合にのみ機能する。5.8.0以前、
 あるいはスレッドが有効になっていない場合は、使うことができない。

                  *************************

The Thread::Rand module allows you to create repeatable random sequences between different threads. Without it, repeatable random sequences can only be created within a thread.

Thread::Randモジュールを使うと、異なるスレッド間で再現可能な乱数列を つくりだせる。このモジュールを使わないと、再現可能な乱数列は一つの スレッドの中でのみ生成される。

サブルーチン

There are only two subroutines.

二つのサブルーチンがあるだけだ。

rand

 my $value = rand();          # 0から1の間の値

 my $value = rand( number );  # 0からnumber-1を含む間の値

The "rand" subroutine functions exactly the same as the normal rand() function.

"rand"サブルーチンは通常のrand()関数と全く同じように機能する。

srand

 srand( usethis );

The "srand" subroutine functions exactly the same as the normal srand() function.

"srand"サブルーチンは通常のsrand()関数と全く同じように機能する。

クラスメソッド

There is one class method.

一つのクラスメソッドがある。

global

 use Thread::Rand ();
 BEGIN {Thread::Rand->global}

The "global" class method allows you to replace the rand() and srand() system functions in all programs by the version supplied by Thread::Rand. To ensure that the right subroutines are called, you must call this class method from within a BEGIN {} block.

"global"クラスメソッドは、全プログラム中のrand()およびsrand()システム関数を Thread::Randが提供するバージョンに置き換えてくれる。正しいサブルーチンを 呼び出すことを保証するために、BEGIN {} ブロック内でこのクラスメソッドを 呼び出さなければならない

必要なモジュール

 load (どのバージョンでも)
 Thread::Tie (0.09)

警告

A bug in Perl 5.8.0 causes random sequences to be identical in threads if the rand() function was called in the parent thread. You can circumvent this problem by adding a CLONE subroutine thus:

Perl 5.8.0のバグにより、親スレッドでrand()関数を呼び出すと、乱数列は 子スレッド内で同じになってしまう。CLONEサブルーチンを以下のように加える ことで、この問題を回避できる。

 sub CLONE { srand() } # 5.8.0のバグに対して必要な処置

This will make sure that each thread gets its own unique seed and therefore its own unique sequence of random numbers. Alternately, you could also solve this with Thread::Rand and a call to the global class method thus:

これにより、各スレッドはそれぞれ一意の種を得ることが保証され、ゆえに 一意な乱数の連続を得られる。これとは別に、Thread::Randとglobal クラスメソッドを使って、この問題を解決できる:

 use Thread::Rand ();
 BEGIN {Thread::Rand->global}

You should however keep monitoring whether future versions of Perl will have this problem fixed. You can then take these circumventions out again.

しかし、将来のPerlのバージョンではこの問題が修正されているかどうか 観察し続けたほうが良い。修正されたときにはこの回避方法を取り除ける。

作者

Elizabeth Mattijsen, <liz@dijkmat.nl>.

Please report bugs to <perlbugs@dijkmat.nl>.

著作権

Copyright (c) 2002-2003 Elizabeth Mattijsen <liz@dijkmat.nl>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

参考

Thread::Tie.