| 1 | package Strong::Random; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | Strong::Random - Strong random generator | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | use Strong::Random; | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | my $rand = new Strong::Random; | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | # Print random character | ||||||||||||||||||||
| 14 | print chr $rand->rand(256) . "\n"; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 2 | ▶ | use 5.022; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 2 | ▶ | use Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 2 | ▶ | use Time::HiRes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | our $VERSION = '0.96'; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 METHODS | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head2 new() | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Strong::Random constructor. | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | Parameters: | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | $seed - Seed for next series of random generation by algorythm. | ||||||||||||||||||||
| 35 | If not specified, will be use CPU-time + PID. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | Return: | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | Strong::Random based class object. | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 44 | 1 | my $class = shift; | |||||||||||||||||||
| 45 | 1 | my $seed = shift; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | my $self = bless {}, $class; | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 1 | ▶ | $self->srand($seed ? abs $seed : undef); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 1 | return $self; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 1 | ▶ | sub _shuffle_seed { | ||||||||||||||||||
|
Subroutine
_shuffle_seed: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 57 | 1 | my $class = shift; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 1 | srand(); | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | return rand() * ((join '.', reverse Time::HiRes::gettimeofday) . $$); | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | sub _harmonic { | ||||||||||||||||||
|
Subroutine
_harmonic: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 65 | 1 | my $self = shift; | |||||||||||||||||||
| 66 | 1 | my $x = $self->{seed}; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 1 | state $half_freq = 2/7.0; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | my $step = int ($x / $half_freq); | |||||||||||||||||||
| 71 | 1 | $x -= $step * $half_freq; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | ▶ | return ($step % 2 ? -1 : 1)*sqrt (1 - $x*$x); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =head2 srand() | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Setup new seed. | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Parameters: | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | $seed - Seed for next series of random generation by algorythm. | ||||||||||||||||||||
| 83 | If not specified, will be use CPU-time + PID. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 1 | ▶ | sub srand { | ||||||||||||||||||
|
Subroutine
srand: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||
| 89 | 1 | my $self = shift; | |||||||||||||||||||
| 90 | 1 | ▶ | my $seed = shift // $self->_shuffle_seed(); | ||||||||||||||||||
|
Condition33%1/3
shift() // $self->_shuffle_seed
MC/DC0%0/2
shift() // $self->_shuffle_seed
shift()
$self->_shuffle_seed
Truth table33%1/3
A // B
A= shift()B= $self->_shuffle_seed
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1 | ▶ | while ($seed >= 100000000) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $seed /= 2; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 1 | $self->{seed} = $seed; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head2 rand() | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Return random number. | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | Parameters: | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | $num - If specified, method will return int between [0..$num-1). | ||||||||||||||||||||
| 106 | If not specfied, method return value form [0..1). | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1 | ▶ | sub rand { | ||||||||||||||||||
|
Subroutine
rand: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 111 | 1 | my $self = shift; | |||||||||||||||||||
| 112 | 1 | my $num = shift; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 1 | my $r = ($self->_harmonic()+1.0)/2.0+1.5; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 1 | $self->{seed} *= $r; | |||||||||||||||||||
| 117 | 1 | ▶ | $self->{seed} /= 2 while ($self->{seed} >= 100000000); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 1 | my $s = sprintf("%0.42g", $r); | |||||||||||||||||||
| 120 | 1 | $s =~ s/\.//; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 1 | ▶ | $r *= substr($s, -5) || 0; | ||||||||||||||||||
|
Condition50%1/2
substr($s, -5) || 0
MC/DC0%0/1
substr($s, -5) || 0
substr($s, -5)
Truth table50%1/2
A
A= substr($s, -5)
| |||||||||||||||||||||
| 123 | 1 | $r = substr($r, 0, 22); | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | $r =~ s/\.//; | |||||||||||||||||||
| 126 | 1 | $r = '0.'.$r; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 1 | ▶ | return $num ? int $num * $r : $r; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 VERSION | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0.96 | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head1 AUTHOR | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | All code writen by Ivan Trunaev. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =head1 LICENSE | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | Commercial support by Author for Open Source! | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | Contacts <ivan@trunaev.ru> | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 2 | 1; | |||||||||||||||||||