| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Math::Random::MT; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 9 |  |  | 9 |  | 236892 | use strict; | 
|  | 9 |  |  |  |  | 24 |  | 
|  | 9 |  |  |  |  | 259 |  | 
| 4 | 9 |  |  | 9 |  | 45 | use Carp; | 
|  | 9 |  |  |  |  | 18 |  | 
|  | 9 |  |  |  |  | 550 |  | 
| 5 | 9 |  |  | 9 |  | 50 | use DynaLoader; | 
|  | 9 |  |  |  |  | 25 |  | 
|  | 9 |  |  |  |  | 278 |  | 
| 6 | 9 |  |  | 9 |  | 41 | use vars qw( @ISA $VERSION ); | 
|  | 9 |  |  |  |  | 16 |  | 
|  | 9 |  |  |  |  | 4098 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | my $gen = undef; | 
| 9 |  |  |  |  |  |  | @ISA = qw( DynaLoader ); | 
| 10 |  |  |  |  |  |  | $VERSION = '1.17'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | bootstrap Math::Random::MT $VERSION; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub new | 
| 15 |  |  |  |  |  |  | { | 
| 16 | 18 |  |  | 18 | 1 | 79 | my ($class, @seeds) = @_; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 18 |  |  |  |  | 124 | my $self = Math::Random::MT::init(); | 
| 19 | 18 |  |  |  |  | 65 | $self->set_seed(@seeds); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 18 |  |  |  |  | 70 | return $self; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub set_seed | 
| 25 |  |  |  |  |  |  | { | 
| 26 | 19 |  |  | 19 | 1 | 40 | my ($self, @seeds) = @_; | 
| 27 | 19 | 100 |  |  |  | 264 | @seeds > 1 ? $self->setup_array(@seeds) : | 
|  |  | 100 |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | $self->init_seed(defined $seeds[0] ? $seeds[0] : _rand_seed()); | 
| 29 | 19 |  |  |  |  | 85 | return $self->get_seed; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub srand | 
| 33 |  |  |  |  |  |  | { | 
| 34 | 10 |  |  | 10 | 1 | 44 | my (@seeds) = @_; | 
| 35 | 10 |  |  |  |  | 53 | $gen = Math::Random::MT->new(@seeds); | 
| 36 | 10 |  |  |  |  | 76 | return $gen->get_seed; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub rand | 
| 40 |  |  |  |  |  |  | { | 
| 41 | 36 |  |  | 36 | 1 | 3816 | my ($self, $N) = @_; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 36 | 100 |  |  |  | 101 | unless (ref $self) { | 
| 44 | 20 |  |  |  |  | 28 | $N = $self; | 
| 45 | 20 | 100 |  |  |  | 49 | Math::Random::MT::srand() unless defined $gen; | 
| 46 | 20 |  |  |  |  | 30 | $self = $gen; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 36 |  | 100 |  |  | 430 | return ($N || 1) * $self->genrand(); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub irand | 
| 53 |  |  |  |  |  |  | { | 
| 54 | 24 |  |  | 24 | 1 | 4423 | my ($self) = @_; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 24 | 100 |  |  |  | 89 | unless (ref $self) { | 
| 57 | 8 | 50 |  |  |  | 23 | Math::Random::MT::srand() unless defined $gen; | 
| 58 | 8 |  |  |  |  | 15 | $self = $gen; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 24 |  |  |  |  | 138 | return $self->genirand(); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # Generate a random seed using the built-in PRNG. | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub _rand_seed { | 
| 68 | 9 |  |  | 9 |  | 17 | my ($self) = @_; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | # Get a seed at random through Perl's CORE::rand(). We do not call | 
| 71 |  |  |  |  |  |  | # CORE::srand() to avoid altering the random numbers that other parts of | 
| 72 |  |  |  |  |  |  | # the running script might be using. The seeds obtained by rapid calls to | 
| 73 |  |  |  |  |  |  | # the _rand_seed() function are all different. | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 9 |  |  |  |  | 303 | return int(CORE::rand(2**32)); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub import | 
| 79 |  |  |  |  |  |  | { | 
| 80 | 9 |  |  | 9 |  | 49 | no strict 'refs'; | 
|  | 9 |  |  |  |  | 16 |  | 
|  | 9 |  |  |  |  | 961 |  | 
| 81 | 9 |  |  | 9 |  | 78 | my $pkg = caller; | 
| 82 | 9 |  |  |  |  | 30 | foreach my $sym (@_) { | 
| 83 | 21 | 100 | 100 |  |  | 235 | if ($sym eq 'srand' || $sym eq 'rand' || $sym eq 'irand') { | 
|  |  |  | 100 |  |  |  |  | 
| 84 | 12 |  |  |  |  | 31 | *{"${pkg}::$sym"} = \&$sym; | 
|  | 12 |  |  |  |  | 120 |  | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | 1; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | __END__ |