| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | Data::Entropy::Algorithms - basic entropy-using algorithms | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | use Data::Entropy::Algorithms | ||||||||||||||||||||||||||||||||||||||||||
| 8 | qw(rand_bits rand_int rand_prob); | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | $str = rand_bits(17); | ||||||||||||||||||||||||||||||||||||||||||
| 11 | $i = rand_int(12345); | ||||||||||||||||||||||||||||||||||||||||||
| 12 | $i = rand_int(Math::BigInt->new("1000000000000")); | ||||||||||||||||||||||||||||||||||||||||||
| 13 | $j = rand_prob(1, 2, 3); | ||||||||||||||||||||||||||||||||||||||||||
| 14 | $j = rand_prob([ 1, 2, 3 ]); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | use Data::Entropy::Algorithms qw(rand_fix rand rand_flt); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | $x = rand_fix(48); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | $x = rand(7); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | $x = rand_flt(0.0, 7.0); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | use Data::Entropy::Algorithms | ||||||||||||||||||||||||||||||||||||||||||
| 23 | qw(pick pick_r choose choose_r shuffle shuffle_r); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | $item = pick($item0, $item1, $item2); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | $item = pick_r(\@items); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | @chosen = choose(3, $item0, $item1, $item2, $item3, $item4); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | $chosen = choose_r(3, \@items); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | @shuffled = shuffle($item0, $item1, $item2, $item3, $item4); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | $shuffled = shuffle_r(\@items); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | This module contains a collection of fundamental algorithms that | ||||||||||||||||||||||||||||||||||||||||||
| 35 | use entropy. They all use the entropy source mechanism described in | ||||||||||||||||||||||||||||||||||||||||||
| 36 | L<Data::Entropy>. | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | package Data::Entropy::Algorithms; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 10 | ▶ | { use 5.006; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 10 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 10 | ▶ | use Data::Entropy qw(entropy_source); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | use Data::Float 0.010 qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | have_subnormal min_normal_exp significand_bits | ||||||||||||||||||||||||||||||||||||||||||
| 50 | float_is_finite float_parts float_sign mult_pow2 copysign | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 10 | ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 10 | ▶ | use Params::Classify 0.000 qw(is_ref); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | our $VERSION = "0.010"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | ▶ | use parent "Exporter"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||
| 58 | rand_bits rand_int rand_prob | ||||||||||||||||||||||||||||||||||||||||||
| 59 | rand_fix rand rand_flt | ||||||||||||||||||||||||||||||||||||||||||
| 60 | pick_r pick choose_r choose shuffle_r shuffle | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | All of these functions use entropy. The entropy source is not an | ||||||||||||||||||||||||||||||||||||||||||
| 66 | explicit input in any case. All functions use the current entropy source | ||||||||||||||||||||||||||||||||||||||||||
| 67 | maintained by the C<Data::Entropy> module. To select an entropy source | ||||||||||||||||||||||||||||||||||||||||||
| 68 | use the C<with_entropy_source> function in that module, or alternatively | ||||||||||||||||||||||||||||||||||||||||||
| 69 | do nothing to use the default source. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head2 Fundamental entropy extraction | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =item rand_bits(NBITS) | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | Returns NBITS bits of entropy, as a string of octets. If NBITS is | ||||||||||||||||||||||||||||||||||||||||||
| 78 | not a multiple of eight then the last octet in the string has its most | ||||||||||||||||||||||||||||||||||||||||||
| 79 | significant bits set to zero. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 42 | ▶ | sub rand_bits($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rand_bits: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 42 | my($nbits) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 42 | ▶ | croak "need a non-negative number of bits to dispense" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | unless $nbits >= 0; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 41 | return entropy_source->get_bits($nbits); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =item rand_int(LIMIT) | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | LIMIT must be a positive integer. Returns a uniformly-distributed random | ||||||||||||||||||||||||||||||||||||||||||
| 93 | integer in the range [0, LIMIT). LIMIT may be either a native integer, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | a C<Math::BigInt> object, or an integer-valued C<Math::BigRat> object; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | the returned number is of the same type. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 20671 | ▶ | sub rand_int($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rand_int: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 20671 | my($limit) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 20671 | ▶ | croak "need a positive upper limit for random variable" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | unless $limit > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 20670 | return entropy_source->get_int($limit); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =item rand_prob(PROB ...) | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =item rand_prob(PROBS) | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Returns a random integer selected with non-uniform probability. The | ||||||||||||||||||||||||||||||||||||||||||
| 111 | relative probabilities are supplied as a list of non-negative integers | ||||||||||||||||||||||||||||||||||||||||||
| 112 | (multiple PROB arguments) or a reference to an array of integers (the | ||||||||||||||||||||||||||||||||||||||||||
| 113 | PROBS argument). The relative probabilities may be native integers, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | C<Math::BigInt> objects, or integer-valued C<Math::BigRat> objects; | ||||||||||||||||||||||||||||||||||||||||||
| 115 | they must all be of the same type. At least one probability value must | ||||||||||||||||||||||||||||||||||||||||||
| 116 | be positive. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | The first relative probability value (the first PROB or the first element | ||||||||||||||||||||||||||||||||||||||||||
| 119 | of PROBS) is the relative probability of returning 0. The absolute | ||||||||||||||||||||||||||||||||||||||||||
| 120 | probability of returning 0 is this value divided by the total of all | ||||||||||||||||||||||||||||||||||||||||||
| 121 | the relative probability values. Similarly the second value controls | ||||||||||||||||||||||||||||||||||||||||||
| 122 | the probability of returning 1, and so on. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 154 | ▶ | sub rand_prob(@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rand_prob: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 154 | ▶ | my $probs = @_ == 1 && is_ref($_[0], "ARRAY") ? $_[0] : \@_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and CORE::rand $_[0]
MC/DC100%2/2
@_ == 1 and CORE::rand $_[0]
@_ == 1
CORE::rand $_[0]
Truth table100%3/3
A and B
A= @_ == 1B= CORE::rand $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 154 | my $total = 0; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 154 | ▶ | for(my $i = @$probs; $i--; ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 754 | my $prob = $probs->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 754 | ▶ | croak "probabilities must be non-negative" unless $prob >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 753 | $total += $prob; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 153 | ▶ | croak "can't have nothing possible" if $total == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 152 | ▶ | for(my $i = @$probs; $i--; ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 370 | my $prob = $probs->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 370 | $total -= $prob; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 370 | ▶ | return $i if entropy_source->get_prob($total, $prob); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =head2 Numbers | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =item rand_fix(NBITS) | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | Returns a uniformly-distributed random NBITS-bit fixed-point fraction in | ||||||||||||||||||||||||||||||||||||||||||
| 151 | the range [0, 1). That is, the result is a randomly-chosen multiple of | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 2^-NBITS, the multiplier being a random integer in the range [0, 2^NBITS). | ||||||||||||||||||||||||||||||||||||||||||
| 153 | The value is returned in the form of a native floating point number, so | ||||||||||||||||||||||||||||||||||||||||||
| 154 | NBITS can be at most one greater than the number of bits of significand | ||||||||||||||||||||||||||||||||||||||||||
| 155 | in the floating point format. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | With NBITS = 48 the range of output values is the same as that of the | ||||||||||||||||||||||||||||||||||||||||||
| 158 | Unix C<drand48> function. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 516 | ▶ | sub rand_fix($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rand_fix: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 516 | my($nbits) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 516 | ▶ | croak "need a non-negative number of bits to dispense" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | unless $nbits >= 0; | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 515 | ▶ | croak "can't generate more than ".(significand_bits+1). | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | " bits of fixed-point fraction" | ||||||||||||||||||||||||||||||||||||||||||
| 168 | if $nbits > significand_bits+1; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 515 | my $frac = 0.0; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 515 | ▶ | for(my $pos = 24; $pos <= $nbits; $pos += 24) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 516 | $frac += mult_pow2(rand_int(1 << 24), -$pos); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 515 | $frac += mult_pow2(rand_int(1 << ($nbits % 24)), -$nbits); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 515 | return $frac; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =item rand([LIMIT]) | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | Generates a random fixed-point fraction by C<rand_fix> and then multiplies | ||||||||||||||||||||||||||||||||||||||||||
| 180 | it by LIMIT, returning the result. LIMIT defaults to 1, and if it | ||||||||||||||||||||||||||||||||||||||||||
| 181 | is 0 then that is also treated as 1. The length of the fixed-point | ||||||||||||||||||||||||||||||||||||||||||
| 182 | fraction is 48 bits, unless that can't be represented in the native | ||||||||||||||||||||||||||||||||||||||||||
| 183 | floating point type, in which case the longest possible fraction will | ||||||||||||||||||||||||||||||||||||||||||
| 184 | be generated instead. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | This is a drop-in replacement for C<CORE::rand>: it produces exactly the | ||||||||||||||||||||||||||||||||||||||||||
| 187 | same range of output values, but using the current entropy source instead | ||||||||||||||||||||||||||||||||||||||||||
| 188 | of a sucky PRNG with linear relationships between successive outputs. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | (C<CORE::rand> does the type of calculation described, but using the | ||||||||||||||||||||||||||||||||||||||||||
| 190 | PRNG C<drand48> to generate the fixed-point fraction.) The details of | ||||||||||||||||||||||||||||||||||||||||||
| 191 | behaviour may change in the future if the behaviour of C<CORE::rand> | ||||||||||||||||||||||||||||||||||||||||||
| 192 | changes, to maintain the match. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | Where the source of a module can't be readily modified, it can be made | ||||||||||||||||||||||||||||||||||||||||||
| 195 | to use this C<rand> by an incantation such as | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | *Foreign::Module::rand = \&Data::Entropy::Algorithms::rand; | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | This must be done before the module is loaded, most likely in a C<BEGIN> | ||||||||||||||||||||||||||||||||||||||||||
| 200 | block. It is also possible to override C<CORE::rand> for all modules, | ||||||||||||||||||||||||||||||||||||||||||
| 201 | by performing this similarly early: | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | *CORE::GLOBAL::rand = \&Data::Entropy::Algorithms::rand; | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | This function should not be used in any new code, because the kind | ||||||||||||||||||||||||||||||||||||||||||
| 206 | of output supplied by C<rand> is hardly ever the right thing to use. | ||||||||||||||||||||||||||||||||||||||||||
| 207 | The C<int(rand($n))> idiom to generate a random integer has non-uniform | ||||||||||||||||||||||||||||||||||||||||||
| 208 | probabilities of generating each possible value, except when C<$n> is a | ||||||||||||||||||||||||||||||||||||||||||
| 209 | power of two. For floating point numbers, C<rand> can't generate most | ||||||||||||||||||||||||||||||||||||||||||
| 210 | representable numbers in its output range, and the output is biased | ||||||||||||||||||||||||||||||||||||||||||
| 211 | towards zero. In new code use C<rand_int> to generate integers and | ||||||||||||||||||||||||||||||||||||||||||
| 212 | C<rand_flt> to generate floating point numbers. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 10 | ▶ | use constant RAND_NBITS => 48 > significand_bits+1 ? significand_bits+1 : 48; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 258 | ▶ | sub rand(;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rand: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 258 | my($limit) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 258 | ▶ | return rand_fix(RAND_NBITS) * | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $limit or $limit == 0
MC/DC100%2/2
not defined $limit or $limit == 0
not defined $limit
$limit == 0
Truth table100%3/3
not(A) or B
A= not defined $limitB= $limit == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | (!defined($limit) || $limit == 0.0 ? 1.0 : $limit); | ||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | =item rand_flt(MIN, MAX) | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | Selects a uniformly-distributed real number (with infinite precision) | ||||||||||||||||||||||||||||||||||||||||||
| 227 | in the range [MIN, MAX] and then rounds this number to the nearest | ||||||||||||||||||||||||||||||||||||||||||
| 228 | representable floating point value, which it returns. (Actually it is | ||||||||||||||||||||||||||||||||||||||||||
| 229 | only I<as if> the function worked this way: in fact it never generates | ||||||||||||||||||||||||||||||||||||||||||
| 230 | the number with infinite precision. It selects between the representable | ||||||||||||||||||||||||||||||||||||||||||
| 231 | floating point values with the probabilities implied by this process.) | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | This can return absolutely any floating point value in the range [MIN, | ||||||||||||||||||||||||||||||||||||||||||
| 234 | MAX]; both MIN and MAX themselves are possible return values. All bits | ||||||||||||||||||||||||||||||||||||||||||
| 235 | of the floating point type are filled randomly, so the range of values | ||||||||||||||||||||||||||||||||||||||||||
| 236 | that can be returned depends on the details of the floating point format. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | (See L<Data::Float> for low-level floating point utilities.) | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | The function C<die>s if MIN and MAX are not both finite. If MIN is | ||||||||||||||||||||||||||||||||||||||||||
| 240 | greater than MAX then their roles are swapped: the order of the limit | ||||||||||||||||||||||||||||||||||||||||||
| 241 | parameters actually doesn't matter. If the limits are identical then | ||||||||||||||||||||||||||||||||||||||||||
| 242 | that value is always returned. As a special case, if the limits are | ||||||||||||||||||||||||||||||||||||||||||
| 243 | positive zero and negative zero then a zero will be returned with a | ||||||||||||||||||||||||||||||||||||||||||
| 244 | randomly-chosen sign. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 11000 | ▶ | sub rand_flt($$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rand_flt: called CC=35 SCAR=40.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 11000 | my($a, $b) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 11000 | ▶ | croak "bounds for rand_flt() must be finite" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&float_is_finite($a) and &float_is_finite($b)
MC/DC0%0/2
&float_is_finite($a) and &float_is_finite($b)
&float_is_finite($a)
&float_is_finite($b)
Truth table33%1/3
A and B
A= &float_is_finite($a)B= &float_is_finite($b)
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | unless float_is_finite($a) && float_is_finite($b); | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 11000 | ▶ | if($a == $b) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 2000 | ▶ | return $_[rand_int(2)] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$a == 0 and &float_sign($a) ne &float_sign($b)
MC/DC50%1/2
$a == 0 and &float_sign($a) ne &float_sign($b)
$a == 0
&float_sign($a) ne &float_sign($b)
Truth table66%2/3
A and B
A= $a == 0B= &float_sign($a) ne &float_sign($b)
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | if $a == 0.0 && float_sign($a) ne float_sign($b); | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 1000 | return $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 9000 | ▶ | ($a, $b) = ($b, $a) if abs($a) < abs($b); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 9000 | ▶ | my($prm_sign, $prm_max_exp, $prm_max_sgnf) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | $a == 0.0 ? ("+", min_normal_exp, 0.0) : float_parts($a); | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 9000 | ▶ | my($b_sign, $b_exp, $b_sgnf) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | $b == 0.0 ? ("+", min_normal_exp, 0.0) : float_parts($b); | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 9000 | my($min_exp, $min_sgnf); | |||||||||||||||||||||||||||||||||||||||||
| 263 | 9000 | my($opp_max_exp, $opp_max_sgnf); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 9000 | ▶ | if($b_sign eq $prm_sign) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 6000 | ($min_exp, $min_sgnf) = ($b_exp, $b_sgnf); | |||||||||||||||||||||||||||||||||||||||||
| 266 | 6000 | ($opp_max_exp, $opp_max_sgnf) = (min_normal_exp, 0.0); | |||||||||||||||||||||||||||||||||||||||||
| 267 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 3000 | ($min_exp, $min_sgnf) = (min_normal_exp, 0.0); | |||||||||||||||||||||||||||||||||||||||||
| 269 | 3000 | ($opp_max_exp, $opp_max_sgnf) = ($b_exp, $b_sgnf); | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 9000 | TRY_AGAIN: | |||||||||||||||||||||||||||||||||||||||||
| 272 | my $exp = $prm_max_exp; | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 9000 | my $bdone = significand_bits; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 9000 | ▶ | $bdone = 28 if $bdone > 28; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 9000 | my $prm_frng = $prm_max_sgnf * (1 << $bdone); | |||||||||||||||||||||||||||||||||||||||||
| 276 | 9000 | ▶ | if($prm_max_sgnf < 1.0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | # subnormal limit | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my $desired_rng = 1 << $bdone; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | while($bdone != significand_bits) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | $bdone++; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | $prm_frng += $prm_frng; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | last if $prm_frng >= $desired_rng; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 9000 | my $prm_rng = int($prm_frng); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 9000 | ▶ | $prm_rng++ if $prm_frng != $prm_rng; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 9000 | my $min_b = $bdone - ($exp - $min_exp); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 9000 | ▶ | my $min_rng = $min_b >= 0 ? int(mult_pow2($min_sgnf, $min_b)) : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 9000 | my $opp_b = $bdone - ($exp - $opp_max_exp); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 9000 | ▶ | my $opp_frng = $opp_b >= 0 ? mult_pow2($opp_max_sgnf, $opp_b) : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 9000 | my $opp_rng = int($opp_frng); | |||||||||||||||||||||||||||||||||||||||||
| 292 | 9000 | ▶ | $opp_rng++ if $opp_frng != $opp_rng; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 9000 | my $n = $min_rng + rand_int($prm_rng - $min_rng + $opp_rng); | |||||||||||||||||||||||||||||||||||||||||
| 294 | 9000 | my($sg, $max_exp, $max_sgnf) = ($a, $prm_max_exp, $prm_max_sgnf); | |||||||||||||||||||||||||||||||||||||||||
| 295 | 9000 | ▶ | if($n >= $prm_rng) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 1173 | $n -= $prm_rng; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 1173 | ($sg, $max_exp, $max_sgnf) = ($b, $opp_max_exp, $opp_max_sgnf); | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 9000 | ▶ | while($n == 0 && $exp - $bdone - 1 >= min_normal_exp) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n == 0 and $exp - $bdone - 1 >= -1022
MC/DC0%0/2
$n == 0 and $exp - $bdone - 1 >= -1022
$n == 0
$exp - $bdone - 1 >= -1022
Truth table33%1/3
A and B
A= $n == 0B= $exp - $bdone - 1 >= -1022
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | $exp -= $bdone + 1; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | $n = rand_int(2 << $bdone); | |||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 9000 | ▶ | for(my $bit = 16; $bit; $bit >>= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 45000 | ▶ | if($bdone >= $bit && $exp - $bit >= min_normal_exp && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$bdone >= $bit and $exp - $bit >= -1022 and $n < 2 << $bdone - $bit
$bdone >= $bit and $exp - $bit >= -1022
MC/DC33%1/3
$bdone >= $bit and $exp - $bit >= -1022 and $n < 2 << $bdone - $bit
$bdone >= $bit
$exp - $bit >= -1022
$n < 2 << $bdone - $bit
Truth table50%2/4
A and B and C
A= $bdone >= $bitB= $exp - $bit >= -1022C= $n < 2 << $bdone - $bit
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | $n < (2 << ($bdone - $bit))) { | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 8546 | $bdone -= $bit; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 8546 | $exp -= $bit; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 9000 | ▶ | goto TRY_AGAIN if $exp < $min_exp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 9000 | ▶ | my $top_sgnf = $exp == $max_exp ? $max_sgnf : 2.0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 9000 | ▶ | my $bot_sgnf = $exp == $min_exp ? $min_sgnf : 1.0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 9000 | my $sgnf = mult_pow2($n, -$bdone); | |||||||||||||||||||||||||||||||||||||||||
| 314 | 9000 | if(!have_subnormal && $exp == min_normal_exp && $sgnf < 1.0) { | |||||||||||||||||||||||||||||||||||||||||
| 315 | $top_sgnf = 1.0; | ||||||||||||||||||||||||||||||||||||||||||
| 316 | $sgnf = 0.0; | ||||||||||||||||||||||||||||||||||||||||||
| 317 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 9000 | $bot_sgnf = 1.0 if !have_subnormal && $exp == min_normal_exp && | |||||||||||||||||||||||||||||||||||||||||
| 319 | $bot_sgnf < 1.0; | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 9000 | ▶ | while($bdone != significand_bits) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 9330 | my $bseg = significand_bits - $bdone; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 9330 | ▶ | $bseg = 28 if $bseg > 28; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 9330 | $bdone += $bseg; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 9330 | $sgnf += mult_pow2(rand_int(1 << $bseg), -$bdone); | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 9000 | ▶ | goto TRY_AGAIN if $sgnf < $bot_sgnf || $sgnf >= $top_sgnf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sgnf < $bot_sgnf or $sgnf >= $top_sgnf
MC/DC0%0/2
$sgnf < $bot_sgnf or $sgnf >= $top_sgnf
$sgnf < $bot_sgnf
$sgnf >= $top_sgnf
Truth table33%1/3
A or B
A= $sgnf < $bot_sgnfB= $sgnf >= $top_sgnf
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 9000 | ▶ | $sgnf = $top_sgnf if $sgnf == $bot_sgnf && rand_int(2); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sgnf == $bot_sgnf and &rand_int(2)
MC/DC0%0/2
$sgnf == $bot_sgnf and &rand_int(2)
$sgnf == $bot_sgnf
&rand_int(2)
Truth table33%1/3
A and B
A= $sgnf == $bot_sgnfB= &rand_int(2)
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 9000 | ▶ | return copysign($sgnf == 0.0 ? 0.0 : mult_pow2($sgnf, $exp), $sg); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | =head2 Combinatorics | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | =item pick(ITEM ...) | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | Randomly selects and returns one of the ITEMs. Each ITEM has equal | ||||||||||||||||||||||||||||||||||||||||||
| 341 | probability of being selected. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =item pick_r(ITEMS) | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | ITEMS must be a reference to an array. Randomly selects and returns | ||||||||||||||||||||||||||||||||||||||||||
| 346 | one of the elements of the array. Each element has equal probability | ||||||||||||||||||||||||||||||||||||||||||
| 347 | of being selected. | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | This is the same operation as that performed by C<pick>, but using | ||||||||||||||||||||||||||||||||||||||||||
| 350 | references to avoid expensive copying of arrays. | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | 155 | ▶ | sub pick_r($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick_r: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 155 | my($a) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 155 | ▶ | croak "need a non-empty array to pick from" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
CORE::rand $a and @$a
MC/DC100%2/2
CORE::rand $a and @$a
CORE::rand $a
@$a
Truth table100%3/3
A and B
A= CORE::rand $aB= @$a
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | unless is_ref($a, "ARRAY") && @$a; | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 152 | return $a->[rand_int(@$a)]; | |||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | 77 | ▶ | sub pick(@) { pick_r(\@_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =item choose(NCHOOSE, ITEM ...) | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | Randomly selects NCHOOSE of the ITEMs. Each ITEM has equal probability | ||||||||||||||||||||||||||||||||||||||||||
| 366 | of being selected. The chosen items are returned in a list in the same | ||||||||||||||||||||||||||||||||||||||||||
| 367 | order in which they appeared in the argument list. | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | =item choose_r(NCHOOSE, ITEMS) | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | ITEMS must be a reference to an array. Randomly selects NCHOOSE of | ||||||||||||||||||||||||||||||||||||||||||
| 372 | the elements in the array. Each element has equal probability of being | ||||||||||||||||||||||||||||||||||||||||||
| 373 | selected. Returns a reference to an array containing the chosen items | ||||||||||||||||||||||||||||||||||||||||||
| 374 | in the same order in which they appeared in the input array. | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | This is the same operation as that performed by C<choose>, but using | ||||||||||||||||||||||||||||||||||||||||||
| 377 | references to avoid expensive copying of arrays. | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 51 | ▶ | sub choose_r($$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
choose_r: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 51 | my($nchoose, $a) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 51 | ▶ | croak "need a non-negative number of items to choose" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | unless $nchoose >= 0; | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 49 | ▶ | croak "need a sufficiently large array to pick from" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
CORE::rand $a and @$a >= $nchoose
MC/DC100%2/2
CORE::rand $a and @$a >= $nchoose
CORE::rand $a
@$a >= $nchoose
Truth table100%3/3
A and B
A= CORE::rand $aB= @$a >= $nchoose
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | unless is_ref($a, "ARRAY") && @$a >= $nchoose; | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 46 | my $ntotal = @$a; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 46 | my $nleave = $ntotal - $nchoose; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 46 | my @chosen; | |||||||||||||||||||||||||||||||||||||||||
| 390 | 46 | ▶ | for(my $i = 0; $i != $ntotal; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | 414 | ▶ | if(entropy_source->get_prob($nleave, $nchoose)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 128 | push @chosen, $a->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 393 | 128 | $nchoose--; | |||||||||||||||||||||||||||||||||||||||||
| 394 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 286 | $nleave--; | |||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||
| 398 | 46 | return \@chosen; | |||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 25 | ▶ | sub choose(@) { @{choose_r(shift, \@_)} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
choose: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | =item shuffle(ITEM ...) | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | Reorders the ITEMs randomly, and returns them in a list in random order. | ||||||||||||||||||||||||||||||||||||||||||
| 406 | Each possible order has equal probability. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | =item shuffle_r(ITEMS) | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | ITEMS must be a reference to an array. Reorders the elements of the | ||||||||||||||||||||||||||||||||||||||||||
| 411 | array randomly. Each possible order has equal probability. Returns a | ||||||||||||||||||||||||||||||||||||||||||
| 412 | reference to an array containing the elements in random order. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | This is the same operation as that performed by C<shuffle>, but using | ||||||||||||||||||||||||||||||||||||||||||
| 415 | references to avoid expensive copying of arrays. | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | 19 | ▶ | sub shuffle_r($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shuffle_r: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 420 | 19 | my($a) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 421 | 19 | ▶ | croak "need an array to shuffle" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 422 | unless is_ref($a, "ARRAY"); | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 18 | $a = [ @$a ]; | |||||||||||||||||||||||||||||||||||||||||
| 424 | 18 | ▶ | for(my $i = @$a; $i > 1; ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 126 | my $j = rand_int($i--); | |||||||||||||||||||||||||||||||||||||||||
| 426 | 126 | @{$a}[$i, $j] = @{$a}[$j, $i]; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 18 | return $a; | |||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | 9 | ▶ | sub shuffle(@) { @{shuffle_r(\@_)} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shuffle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | L<Data::Entropy>, | ||||||||||||||||||||||||||||||||||||||||||
| 438 | L<Data::Entropy::Source> | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | Maintained by Robert Rothenberg <perl@rhizomnic.com> | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | Copyright (C) 2006, 2007, 2009, 2011, 2025-2026 | ||||||||||||||||||||||||||||||||||||||||||
| 449 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 454 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||