File Coverage

blib/lib/Math/Random/MT.pm
Criterion Covered Total %
statement 43 43 100.0
branch 11 12 91.6
condition 10 11 90.9
subroutine 12 12 100.0
pod 5 5 100.0
total 81 83 97.5


line stmt bran cond sub pod time code
1             package Math::Random::MT;
2              
3 9     9   337110 use strict;
  9         24  
  9         346  
4 9     9   47 use Carp;
  9         20  
  9         633  
5 9     9   57 use DynaLoader;
  9         24  
  9         320  
6 9     9   46 use vars qw( @ISA $VERSION );
  9         14  
  9         4260  
7              
8             my $gen = undef;
9             @ISA = qw( DynaLoader );
10             $VERSION = '1.16';
11              
12             bootstrap Math::Random::MT $VERSION;
13              
14             sub new
15             {
16 16     16 1 90 my ($class, @seeds) = @_;
17              
18 16         277 my $self = Math::Random::MT::init();
19 16         85 $self->set_seed(@seeds);
20              
21 16         67 return $self;
22             }
23              
24             sub set_seed
25             {
26 17     17 1 38 my ($self, @seeds) = @_;
27 17 100 66     221 @seeds > 1 ? $self->setup_array(@seeds) :
28             $self->init_seed($seeds[0] || _rand_seed());
29 17         80 return $self->get_seed;
30             }
31              
32             sub srand
33             {
34 9     9 1 53 my (@seeds) = @_;
35 9         63 $gen = Math::Random::MT->new(@seeds);
36 9         83 return $gen->get_seed;
37             }
38              
39             sub rand
40             {
41 36     36 1 4664 my ($self, $N) = @_;
42              
43 36 100       109 unless (ref $self) {
44 20         28 $N = $self;
45 20 100       45 Math::Random::MT::srand() unless defined $gen;
46 20         28 $self = $gen;
47             }
48              
49 36   100     504 return ($N || 1) * $self->genrand();
50             }
51              
52             sub irand
53             {
54 24     24 1 6414 my ($self) = @_;
55              
56 24 100       80 unless (ref $self) {
57 8 50       24 Math::Random::MT::srand() unless defined $gen;
58 8         15 $self = $gen;
59             }
60              
61 24         825 return $self->genirand();
62             }
63              
64              
65             # Generate a random seed using the built-in PRNG.
66              
67             sub _rand_seed {
68 9     9   21 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         415 return int(CORE::rand(2**32));
76             }
77              
78             sub import
79             {
80 9     9   54 no strict 'refs';
  9         16  
  9         1092  
81 9     9   82 my $pkg = caller;
82 9         31 foreach my $sym (@_) {
83 21 100 100     235 if ($sym eq 'srand' || $sym eq 'rand' || $sym eq 'irand') {
      100        
84 12         21 *{"${pkg}::$sym"} = \&$sym;
  12         124  
85             }
86             }
87             }
88              
89             1;
90              
91             __END__