| 1 | package Math::Random::Secure; | ||||||||||
| 2 | 5 | $Math::Random::Secure::VERSION = '0.080001'; | |||||||||
| 3 | # ABSTRACT: Cryptographically-secure, cross-platform replacement for rand() | ||||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | ▶ | use Math::Random::Secure::RNG; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | # ISAAC, a 32-bit generator, should only be capable of generating numbers | ||||||||||
| 11 | # between 0 and 2^32 - 1. We want _to_float to generate numbers possibly | ||||||||||
| 12 | # including 0, but always less than 1.0. Dividing the integer produced | ||||||||||
| 13 | # by irand() by this number should do that exactly. | ||||||||||
| 14 | 5 | ▶ | use constant DIVIDE_BY => 2**32; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 5 | our $RNG; | |||||||||
| 17 | |||||||||||
| 18 | 5 | our @EXPORT_OK = qw(rand srand irand); | |||||||||
| 19 | |||||||||||
| 20 | 40000 | ▶ | sub rand (;$) { | ||||||||
|
Subroutine
rand: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 40000 | my ($limit) = @_; | |||||||||
| 22 | 40000 | my $int = irand(); | |||||||||
| 23 | 40000 | return _to_float($int, $limit); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 80605 | ▶ | sub irand (;$) { | ||||||||
|
Subroutine
irand: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 27 | 80605 | my ($limit) = @_; | |||||||||
| 28 | 80605 | ▶ | Math::Random::Secure::srand() if !defined $RNG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 80605 | my $int = $RNG->irand(); | |||||||||
| 30 | 80605 | ▶ | if (defined $limit) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | # We can't just use the mod operator because it will bias | ||||||||||
| 32 | # our output. Search for "modulo bias" on the Internet for | ||||||||||
| 33 | # details. This is slower than mod(), but does not have a bias, | ||||||||||
| 34 | # as demonstrated by our uniform.t test. | ||||||||||
| 35 | 30000 | return int(_to_float($int, $limit)); | |||||||||
| 36 | } | ||||||||||
| 37 | 50605 | return $int; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 17 | ▶ | sub srand (;$) { | ||||||||
|
Subroutine
srand: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 41 | 17 | my ($value) = @_; | |||||||||
| 42 | 17 | ▶ | if (defined $RNG) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 14 | ▶ | if (defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 12 | $RNG->seed($value); | |||||||||
| 45 | } | ||||||||||
| 46 | else { | ||||||||||
| 47 | 2 | $RNG->clear_seed; | |||||||||
| 48 | } | ||||||||||
| 49 | 14 | $RNG->clear_rng; | |||||||||
| 50 | } | ||||||||||
| 51 | else { | ||||||||||
| 52 | 3 | my %args; | |||||||||
| 53 | 3 | ▶ | if (defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $args{seed} = $value; | |||||||||
| 55 | } | ||||||||||
| 56 | 3 | $RNG = Math::Random::Secure::RNG->new(%args); | |||||||||
| 57 | } | ||||||||||
| 58 | # This makes srand return the seed and also makes sure that we | ||||||||||
| 59 | # get the seed right now, if no $value was passed. | ||||||||||
| 60 | 17 | return $RNG->seed; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 70000 | ▶ | sub _to_float { | ||||||||
|
Subroutine
_to_float: called CC=2 SCAR=6.9
| |||||||||||
| 64 | 70000 | my ($integer, $limit) = @_; | |||||||||
| 65 | 70000 | ▶ | $limit = 1 if !$limit; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 70000 | return ($integer / DIVIDE_BY) * $limit; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 5 | __PACKAGE__ | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||