| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Filename: Rand/MersenneTwister.pm | 
| 2 |  |  |  |  |  |  | # Author:   George Schlossnagle | 
| 3 |  |  |  |  |  |  | #           Theo Schlossnagle | 
| 4 |  |  |  |  |  |  | # Created:  03 October 2002 | 
| 5 |  |  |  |  |  |  | # Version:  1.0.1 | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | # Copyright (c) OmniTI Computer Consulting, Inc. All rights reserved. | 
| 8 |  |  |  |  |  |  | #   This program is free software; you can redistribute it and/or | 
| 9 |  |  |  |  |  |  | #   modify it under the same terms as Perl itself. | 
| 10 |  |  |  |  |  |  | # | 
| 11 |  |  |  |  |  |  | # | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | package Rand::MersenneTwister; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | require 5.004; | 
| 16 |  |  |  |  |  |  | require Exporter; | 
| 17 |  |  |  |  |  |  | require DynaLoader; | 
| 18 |  |  |  |  |  |  | require AutoLoader; | 
| 19 | 1 |  |  | 1 |  | 458 | use Carp; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 67 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 1 |  |  | 1 |  | 3 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 22 | 1 |  |  | 1 |  | 4 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD); | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 440 |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | $VERSION = "1.0.1" ; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | @ISA = qw(Exporter DynaLoader); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | %EXPORT_TAGS = ( | 
| 30 |  |  |  |  |  |  | FUNCS => [ qw(mt_init | 
| 31 |  |  |  |  |  |  | mt_free | 
| 32 |  |  |  |  |  |  | mt_seed | 
| 33 |  |  |  |  |  |  | mt_rand) ], | 
| 34 |  |  |  |  |  |  | ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | @EXPORT = qw( ERROR ); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | @EXPORT_OK = qw( | 
| 39 |  |  |  |  |  |  | mt_init | 
| 40 |  |  |  |  |  |  | mt_free | 
| 41 |  |  |  |  |  |  | mt_seed | 
| 42 |  |  |  |  |  |  | mt_rand); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub AUTOLOAD { | 
| 45 |  |  |  |  |  |  | # This AUTOLOAD is used to 'autoload' constants from the constant() | 
| 46 |  |  |  |  |  |  | # XS function.  If a constant is not found then control is passed | 
| 47 |  |  |  |  |  |  | # to the AUTOLOAD in AutoLoader. | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 |  |  | 0 |  | 0 | my $constname; | 
| 50 | 0 |  |  |  |  | 0 | ($constname = $AUTOLOAD) =~ s/.*:://; | 
| 51 | 0 | 0 |  |  |  | 0 | my $val = constant($constname, @_ ? $_[0] : 0); | 
| 52 | 0 | 0 |  |  |  | 0 | if ($! != 0) { | 
| 53 | 0 | 0 |  |  |  | 0 | if ($! =~ /Invalid/) { | 
| 54 | 0 |  |  |  |  | 0 | $AutoLoader::AUTOLOAD = $AUTOLOAD; | 
| 55 | 0 |  |  |  |  | 0 | goto &AutoLoader::AUTOLOAD; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  | else { | 
| 58 | 0 |  |  |  |  | 0 | croak "Your vendor has not defined Rand::MersenneTwister macro $constname"; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  | } | 
| 61 | 0 |  |  |  |  | 0 | eval "sub $AUTOLOAD { \"$val\" }"; | 
| 62 | 0 |  |  |  |  | 0 | goto &$AUTOLOAD; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | bootstrap Rand::MersenneTwister $VERSION ; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub new { | 
| 68 | 1 |  |  | 1 | 0 | 55 | my $self = shift; | 
| 69 | 1 |  | 33 |  |  | 6 | my $class = ref($self) || $self; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 1 |  |  |  |  | 9 | my $td = mt_init(); | 
| 72 | 1 |  |  |  |  | 3 | my $obj = bless { GEN => $td }, $class; | 
| 73 | 1 | 50 |  |  |  | 4 | if(@_) { | 
| 74 | 0 |  |  |  |  | 0 | $obj->seed(@_); | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 1 |  |  |  |  | 2 | return $obj; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub seed { | 
| 80 | 2 |  |  | 2 | 0 | 9 | my($self, $seed) = @_; | 
| 81 | 2 |  | 33 |  |  | 5 | $seed ||= time*$$*100000; | 
| 82 | 2 |  |  |  |  | 22 | mt_seed($self->{GEN}, $seed); | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub rand { | 
| 86 | 20 |  |  | 20 | 0 | 98 | my $self = shift; | 
| 87 | 20 |  |  |  |  | 63 | return mt_rand($self->{GEN}, @_); | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub DESTROY { | 
| 91 | 1 |  |  | 1 |  | 10 | my($self) = shift; | 
| 92 | 1 |  |  |  |  | 94 | mt_free($self->{GEN}); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | 1; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | __END__ |