| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 49 | 0 | require Win32::API; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | state $rand = Win32::API->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | } elsif (-r "/dev/urandom") { | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | open my $urandom, '<:raw', '/dev/urandom' or croak("Couldn't open /dev/urandom: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sysread($urandom, $ret, $count) or croak("Couldn't read from csprng: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 0 | croak("Unknown operating system $^O"); | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if (length($ret) != $count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | 8 | my $j = $self->random_int(0, $i); | |||||||||||||||||||||||||||
| 83 | 8 | ▶ | @shuffled[$i, $j] = @shuffled[$j, $i] if $i != $j; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $num or $num <= 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||