line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
package Math::simpleRNG; |
4
|
|
|
|
|
|
|
# |
5
|
1
|
|
|
1
|
|
755
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
38
|
|
6
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION @ISA @EXPORT_OK); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
297
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
@EXPORT_OK = qw(sRNG); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$VERSION = do { my @r = (q$Revision: 0.04 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
MATH::simpleRNG - simple Random Number Generator |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Math::simpleRNG qw (sRNG); |
21
|
|
|
|
|
|
|
$random = sRNG('seed1','seed2'); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
This function uses George Marsaglia's Multiply With Carry algorithm |
26
|
|
|
|
|
|
|
to produce uniformly distributed unsigned integers. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=over 4 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item * $random = sRNG('seed1','seed2') |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
input: seed1 [optional] |
33
|
|
|
|
|
|
|
seed2 [optional] |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
return: an unsigned random integer 1 -> 2^32 -1 |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Starting from a known set of non-zero seeds, the RNG |
38
|
|
|
|
|
|
|
will return a repeating set of pseudo random numbers. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
You may alter the pattern by periodically supplying |
41
|
|
|
|
|
|
|
additional seed(s). If no seed is supplied or the seed |
42
|
|
|
|
|
|
|
integer portion of the seed is zero, system time() is |
43
|
|
|
|
|
|
|
used to seed the algorithm. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Note: for smaller numbers, i.e. 3-4 digit |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $num = sRNG int(sRNG()/5000000); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=back |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my($m_z,$m_w); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# generate uniformly distributed unsigned integers |
56
|
|
|
|
|
|
|
# http://www.codeproject.com/Articles/25172/Simple-Random-Number-Generation |
57
|
|
|
|
|
|
|
# |
58
|
|
|
|
|
|
|
sub sRNG { |
59
|
999
|
|
|
999
|
1
|
4320
|
my($s1,$s2) = @_; |
60
|
999
|
100
|
66
|
|
|
3882
|
if ($s1 && int($s1)) { |
|
|
50
|
|
|
|
|
|
61
|
1
|
|
|
|
|
2
|
$m_z = int($s1); |
62
|
|
|
|
|
|
|
} elsif (!$m_z) { |
63
|
0
|
|
|
|
|
0
|
$m_z = time(); |
64
|
|
|
|
|
|
|
} |
65
|
999
|
100
|
66
|
|
|
7271
|
if ($s2 && int($s2)) { |
|
|
50
|
|
|
|
|
|
66
|
1
|
|
|
|
|
2
|
$m_w = int($s2); |
67
|
|
|
|
|
|
|
} elsif (!$m_w) { |
68
|
0
|
|
|
|
|
0
|
$m_w = time(); |
69
|
|
|
|
|
|
|
} |
70
|
999
|
|
|
|
|
1567
|
$m_z = 36969 * ($m_z & 0xffff) + ($m_z >> 16); |
71
|
999
|
|
|
|
|
1117
|
$m_w = 18000 * ($m_w & 0xffff) + ($m_w >> 16); |
72
|
|
|
|
|
|
|
# 32 bit integer version of ((($m_z << 16) & 0xffffffff) + ($m_w & 0xffffffff)) & 0xffffffff; |
73
|
999
|
|
|
|
|
2444
|
(((($m_z & 0xffff) + ($m_w >> 16)) & 0xffff) << 16) + ($m_w & 0xffff); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 AUTHOR |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Michael Robinton |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Copyright 2013-2014, Michael Robinton |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This module is licensed under the Code Project Open License (CPOL) |
85
|
|
|
|
|
|
|
a copy of which is included with this distribution. A copy may also be |
86
|
|
|
|
|
|
|
obtained at http://www.codeproject.com/info/cpol10.aspx |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Thanks to John D. Cook for his article on the Simple RNG found here: |
91
|
|
|
|
|
|
|
http://www.codeproject.com/Articles/25172/Simple-Random-Number-Generation |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 EXPORT_OK |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sRNG |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
none |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |