Data::Entropy::Algorithms(3pm) | User Contributed Perl Documentation | Data::Entropy::Algorithms(3pm) |
Data::Entropy::Algorithms - basic entropy-using algorithms
use Data::Entropy::Algorithms qw(rand_bits rand_int rand_prob); $str = rand_bits(17); $i = rand_int(12345); $i = rand_int(Math::BigInt->new("1000000000000")); $j = rand_prob(1, 2, 3); $j = rand_prob([ 1, 2, 3 ]); use Data::Entropy::Algorithms qw(rand_fix rand rand_flt); $x = rand_fix(48); $x = rand(7); $x = rand_flt(0.0, 7.0); use Data::Entropy::Algorithms qw(pick pick_r choose choose_r shuffle shuffle_r); $item = pick($item0, $item1, $item2); $item = pick_r(\@items); @chosen = choose(3, $item0, $item1, $item2, $item3, $item4); $chosen = choose_r(3, \@items); @shuffled = shuffle($item0, $item1, $item2, $item3, $item4); $shuffled = shuffle_r(\@items);
This module contains a collection of fundamental algorithms that use entropy. They all use the entropy source mechanism described in Data::Entropy.
All of these functions use entropy. The entropy source is not an explicit input in any case. All functions use the current entropy source maintained by the "Data::Entropy" module. To select an entropy source use the "with_entropy_source" function in that module, or alternatively do nothing to use the default source.
The first relative probability value (the first PROB or the first element of PROBS) is the relative probability of returning 0. The absolute probability of returning 0 is this value divided by the total of all the relative probability values. Similarly the second value controls the probability of returning 1, and so on.
With NBITS = 48 the range of output values is the same as that of the Unix "drand48" function.
This is a drop-in replacement for "CORE::rand": it produces exactly the same range of output values, but using the current entropy source instead of a sucky PRNG with linear relationships between successive outputs. ("CORE::rand" does the type of calculation described, but using the PRNG "drand48" to generate the fixed-point fraction.) The details of behaviour may change in the future if the behaviour of "CORE::rand" changes, to maintain the match.
Where the source of a module can't be readily modified, it can be made to use this "rand" by an incantation such as
*Foreign::Module::rand = \&Data::Entropy::Algorithms::rand;
This must be done before the module is loaded, most likely in a "BEGIN" block. It is also possible to override "CORE::rand" for all modules, by performing this similarly early:
*CORE::GLOBAL::rand = \&Data::Entropy::Algorithms::rand;
This function should not be used in any new code, because the kind of output supplied by "rand" is hardly ever the right thing to use. The "int(rand($n))" idiom to generate a random integer has non-uniform probabilities of generating each possible value, except when $n is a power of two. For floating point numbers, "rand" can't generate most representable numbers in its output range, and the output is biased towards zero. In new code use "rand_int" to generate integers and "rand_flt" to generate floating point numbers.
This can return absolutely any floating point value in the range [MIN, MAX]; both MIN and MAX themselves are possible return values. All bits of the floating point type are filled randomly, so the range of values that can be returned depends on the details of the floating point format. (See Data::Float for low-level floating point utilities.)
The function "die"s if MIN and MAX are not both finite. If MIN is greater than MAX then their roles are swapped: the order of the limit parameters actually doesn't matter. If the limits are identical then that value is always returned. As a special case, if the limits are positive zero and negative zero then a zero will be returned with a randomly-chosen sign.
This is the same operation as that performed by "pick", but using references to avoid expensive copying of arrays.
This is the same operation as that performed by "choose", but using references to avoid expensive copying of arrays.
This is the same operation as that performed by "shuffle", but using references to avoid expensive copying of arrays.
Data::Entropy, Data::Entropy::Source
Andrew Main (Zefram) <zefram@fysh.org>
Copyright (C) 2006, 2007, 2009, 2011 Andrew Main (Zefram) <zefram@fysh.org>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-06-12 | perl v5.34.0 |