| 1 | #! /usr/math/bin/perl -w |
1 | |
| 2 | |
2 | |
| 3 | |
3 | |
| 4 | package PGrandom; |
4 | package PGrandom; |
| 5 | |
5 | |
| 6 | |
6 | |
| … | |
… | |
| 21 | $seed = mod ($multiplier * $seed + $translate, $modulus); |
21 | $seed = mod ($multiplier * $seed + $translate, $modulus); |
| 22 | my $self = { 'seed' => $seed, |
22 | my $self = { 'seed' => $seed, |
| 23 | 'original_seed' => $original_seed, # this and the next value are largely for debugging |
23 | 'original_seed' => $original_seed, # this and the next value are largely for debugging |
| 24 | 'number_of_calls' => 1 # there is always one call to set the seed. |
24 | 'number_of_calls' => 1 # there is always one call to set the seed. |
| 25 | }; |
25 | }; |
| 26 | |
26 | |
| 27 | bless $self, $class; |
27 | bless $self, $class; |
| 28 | |
28 | |
| 29 | return $self; |
29 | return $self; |
| 30 | } |
30 | } |
| 31 | sub mod { # for some reason perl's % doesn't seem to work for large numbers? |
31 | sub mod { # for some reason perl's % doesn't seem to work for large numbers? |
| 32 | my $a = shift; |
32 | my $a = shift; |
| 33 | my $b = shift; |
33 | my $b = shift; |
| … | |
… | |
| 49 | $out = $begin +$incr*int( ($new_seed/($modulus))*( ($end-$begin)/$incr +1 ) ) ; |
49 | $out = $begin +$incr*int( ($new_seed/($modulus))*( ($end-$begin)/$incr +1 ) ) ; |
| 50 | } else { # if $incr is less than zero return "continuous" distribution |
50 | } else { # if $incr is less than zero return "continuous" distribution |
| 51 | $out = $begin + ($end-$begin)*$new_seed/$modulus; |
51 | $out = $begin + ($end-$begin)*$new_seed/$modulus; |
| 52 | } |
52 | } |
| 53 | $out; |
53 | $out; |
| 54 | |
54 | |
| 55 | } |
55 | } |
| 56 | sub rand { |
56 | sub rand { |
| 57 | my $self = shift; |
57 | my $self = shift; |
| 58 | my $end = shift; |
58 | my $end = shift; |
| 59 | $end = 1 unless defined($end); |
59 | $end = 1 unless defined($end); |