blib/lib/Math/simpleRNG.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 3 / 3 Podpod 100% 1 / 1 totaltotal 82.6% 38 / 46 CC 7 SCAR 18.3
CC
7
Cov
84%
sRNG
7.8
BEGIN
1.0
BEGIN
1.0
CRAP 6.3
↑ Summary
1 #!/usr/bin/perl
2 #
3 package Math::simpleRNG;
4 #
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require Exporter;
8  
9 1 @ISA = qw(Exporter);
10 1 @EXPORT_OK = qw(sRNG);
11  
12 1 $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 1 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 999 ▶sub sRNG {
Subroutine sRNG: called CC=7 SCAR=20.5
Pod documented
59 999 my($s1,$s2) = @_;
60 999 ▶ if ($s1 && int($s1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s1 and int $s1) { } 1 998 999
elsif (not $m_z) { } 0 998 998
Condition66%2/3
$s1 and int $s1
!ll&&!rl&&r
99801
MC/DC50%1/2
$s1 and int $s1
$s1 int $s1
Truth table66%2/3
A and B
A= $s1B= int $s1
ABresult
0X0
100
111
61 1 $m_z = int($s1);
62 } elsif (!$m_z) {
63 0 $m_z = time();
64 }
65 999 ▶ if ($s2 && int($s2)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s2 and int $s2) { } 1 998 999
elsif (not $m_w) { } 0 998 998
Condition66%2/3
$s2 and int $s2
!ll&&!rl&&r
99801
MC/DC50%1/2
$s2 and int $s2
$s2 int $s2
Truth table66%2/3
A and B
A= $s2B= int $s2
ABresult
0X0
100
111
66 1 $m_w = int($s2);
67 } elsif (!$m_w) {
68 0 $m_w = time();
69 }
70 999 $m_z = 36969 * ($m_z & 0xffff) + ($m_z >> 16);
71 999 $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 (((($m_z & 0xffff) + ($m_w >> 16)) & 0xffff) << 16) + ($m_w & 0xffff);
74 }
75  
76 =head1 AUTHOR
77  
78 Michael Robinton <michael@bizsystems.com>
79  
80 =head1 COPYRIGHT & LICENSE
81  
82 Copyright 2013-2014, Michael Robinton <michael@bizsystems.com>
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 1;
↑ Summary