blib/lib/Peta/NN/RNG.pm untested

filter: Statementstmt 0.0% 0 / 19 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 32 CC 2 SCAR 11.5
CC
2
Cov
0%
new
6.0
uniform
2.0
below
2.0
CRAP 3.1
1 package Peta::NN::RNG;
2 # ABSTRACT: seeded random numbers that are the same on every perl
3  
4 # A small seeded generator of our own, not perl's rand(): initial weights and
5 # the shuffle order must be the same on every perl and every run, or a trained
6 # model cannot be reproduced and test thresholds cannot be exact.
7  
8 use v5.36;
9  
10 our $VERSION = '0.2610090';
11  
12 my $MODULUS = 2_147_483_647; # 2**31 - 1, the Park-Miller "minimal standard"
13 my $MULTIPLIER = 48_271; # state * multiplier stays below 2**47: exact in an IV
14 my $TWO_PI = 6.283185307179586;
15  
16 sub new ($class, $seed = 1) {
17 my $state = int($seed) % $MODULUS;
18 $state = 1 if $state <= 0;
19 return bless { state => $state }, $class;
20 }
21  
22 # Uniform in the open interval (0, 1).
23 sub uniform ($self) {
24 $self->{state} = ($self->{state} * $MULTIPLIER) % $MODULUS;
25 return $self->{state} / $MODULUS;
26 }
27  
28 # Uniform integer in 0 .. $n-1.
29 sub below ($self, $n) {
30 return int($self->uniform * $n);
31 }
32  
33 # Standard normal, Box-Muller.
34 sub normal ($self) {
35 return sqrt(-2 * log($self->uniform)) * cos($TWO_PI * $self->uniform);
36 }
37  
38 # Fisher-Yates, in place.
39 sub shuffle ($self, $list) {
40 for my $i (reverse 1 .. $#$list) {
41 my $j = $self->below($i + 1);
42 @$list[$i, $j] = @$list[$j, $i];
43 }
44 return $list;
45 }
46  
47 1;
48  
49 __END__