blib/lib/Math/Random/Xoshiro256.pm

filter: Statementstmt 81.4% 57 / 70 Branchbran 53.3% 16 / 30 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 57.1% 4 / 7 totaltotal 72.3% 89 / 123 CC 17 SCAR 20.2
CC
17
Cov
72%
os_random_bytes
19.0
random_bytes
4.3
new
3.9
CRAP 7.5
« blib/lib/Math/Random/example.pl ↑ Summary Xoshiro256.xs »
1 package Math::Random::Xoshiro256;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # https://pause.perl.org/pause/query?ACTION=pause_operating_model#3_5_factors_considering_in_the_indexing_phase
8 1 our $VERSION = '0.1.2';
9  
10 1 require XSLoader;
11 1 XSLoader::load('Math::Random::Xoshiro256', $VERSION);
12  
13 1 ▶sub new {
Subroutine new: called CC=3 SCAR=13.6
Pod undocumented
14 1 my ($class, $opts) = @_;
15 1 my $self = Math::Random::Xoshiro256::_xs_new($class);
16  
17 # Check if the user passed any seeds into the constructor
18 1 ▶ if (exists $opts->{seed}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts->{'seed'}) { } 0 1 1
elsif (exists $opts->{'seed4'}) { } 0 1 1
19 0 my $seed = $opts->{seed};
20 0 $self->seed($seed);
21 } elsif (exists $opts->{seed4}) {
22 0 my @seeds = @$opts->{seeds};
23 0 $self->seed4(@seeds);
24 } else {
25 1 $self->auto_seed;
26 }
27  
28 1 return $self;
29 }
30  
31 1 ▶sub auto_seed {
Subroutine auto_seed: called CC=1 SCAR=0.0
Pod undocumented
32 1 my ($self) = @_;
33  
34 # Get 32 bytes worth of random bytes and build 4x uint64_t seeds from them
35 1 my $bytes = os_random_bytes(4 * 8);
36 1 my @seeds = unpack('Q4', $bytes);
37  
38 1 $self->seed4(@seeds);
39 }
40  
41 # Fetch random bytes from the OS supplied method
42 # /dev/urandom = Linux, Unix, FreeBSD, Mac, Android
43 # Windows requires the Win32::API call to call RtlGenRandom()
44 1 ▶sub os_random_bytes {
Subroutine os_random_bytes: called CC=8 SCAR=29.4
Pod undocumented
45 1 my $count = shift();
46 1 my $ret = "";
47  
48 1 ▶ if ($^O eq 'MSWin32') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
elsif (-r '/dev/urandom') { } 1 0 1
49 0 require Win32::API;
50  
51 0 ▶ state $rand = Win32::API->new(
Branch: 1 branch
BranchTrueFalseTotal
state $rand = "Win32::API"->new("advapi32", "INT SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength)") or croak("Could not import SystemFunction036: $^E") 0 0 0
52 'advapi32',
53 'INT SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength)'
54 ) or croak("Could not import SystemFunction036: $^E");
55  
56 0 $ret = chr(0) x $count;
57 0 ▶ $rand->Call($ret, $count) or croak("Could not read from csprng: $^E");
Branch: 1 branch
BranchTrueFalseTotal
$rand->Call($ret, $count) or croak("Could not read from csprng: $^E") 0 0 0
58 } elsif (-r "/dev/urandom") {
59 1 ▶ open my $urandom, '<:raw', '/dev/urandom' or croak("Couldn't open /dev/urandom: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $urandom, "<:raw", "/dev/urandom" or croak("Couldn't open /dev/urandom: $!") 1 0 1
60  
61 1 ▶ sysread($urandom, $ret, $count) or croak("Couldn't read from csprng: $!");
Branch: 1 branch
BranchTrueFalseTotal
sysread $urandom, $ret, $count or croak("Couldn't read from csprng: $!") 1 0 1
62 } else {
63 0 croak("Unknown operating system $^O");
64 };
65  
66 1 ▶ if (length($ret) != $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ret != $count) 0 1 1
67 0 croak("Unable to read $count bytes from OS");
68 }
69  
70 1 return $ret;
71 }
72  
73 1 ▶sub shuffle_array {
Subroutine shuffle_array: called CC=3 SCAR=11.0
Pod documented
74 1 my ($self, @array) = @_;
75  
76 # Make a copy of the array to shuffle
77 1 my @shuffled = @array;
78 1 my $n = scalar(@shuffled);
79  
80 # Shuffle the array using the Fisher-Yates algorithm
81 1 ▶ for (my $i = $n - 1; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 8 1 9
82 8 my $j = $self->random_int(0, $i);
83 8 ▶ @shuffled[$i, $j] = @shuffled[$j, $i] if $i != $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i != $j 7 1 8
84 }
85  
86 1 return @shuffled;
87 }
88  
89 1 ▶sub random_elem {
Subroutine random_elem: called CC=2 SCAR=7.2
Pod documented
90 1 my ($self, @array) = @_;
91  
92 1 ▶ if (!@array) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@array) 0 1 1
93 0 return undef;
94 }
95  
96 1 my $idx = $self->random_int(0, scalar(@array) - 1);
97 1 my $ret = $array[$idx];
98  
99 1 return $ret;
100 }
101  
102 2 ▶sub random_bytes {
Subroutine random_bytes: called CC=4 SCAR=14.6
Pod documented
103 2 my ($self, $num) = @_;
104  
105 2 ▶ if (!defined($num) || $num <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $num or $num <= 0) 0 2 2
Condition33%1/3
not defined $num or $num <= 0
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $num or $num <= 0
not defined $num $num <= 0
Truth table33%1/3
not(A) or B
A= not defined $numB= $num <= 0
ABresult
1X1
011
000
106 0 croak("random_bytes: positive number required");
107 }
108  
109 # Get random bytes until we have the desired number
110 2 my $bytes = '';
111 2 ▶ while (length($bytes) < $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes < $num) 3 2 5
112 3 my $rand64 = $self->rand64;
113 3 $bytes .= pack('Q<', $rand64); # little endian for each 64-bit chunk
114 }
115  
116 2 return substr($bytes, 0, $num);
117 }
118  
119 10000 ▶sub random_float {
Subroutine random_float: called CC=2 SCAR=7.1
Pod documented
120 10000 my ($self, $non_inclusive) = @_;
121  
122 # Get a random 64-bit integer and convert it to a float in [0,1]
123 10000 my $u64 = $self->rand64;
124 10000 my $top53 = $u64 >> 11;
125  
126 10000 my $ret;
127 10000 ▶ if ($non_inclusive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($non_inclusive) { } 0 10000 10000
128 0 $ret = $top53 / ((2**53) - 1);
129 } else {
130 10000 $ret = $top53 / (2**53);
131 }
132  
133 10000 return $ret;
134 }
135  
136 1 1;
137 __END__
« blib/lib/Math/Random/example.pl ↑ Summary Xoshiro256.xs »