Game::PseudoRand - pseudo random distribution functions
use Game::PseudoRand qw(prd_step prd_table); ( $coinflip, undef ) = prd_step( start => 0.25, step => 0.41675 ); ( $randfn, undef ) = prd_table( start => 0.12, table => [ 0.05, 0.05, 0.1, 0.1 ] ); $coinflip->() and say "heads";
This module creates Pseudo Random Distribution random functions. For example one may construct a function that returns true 25% of the time but with a rather different distribution than from a typical random function: typically a distribution with much lower odds of long runs of hits or misses.
These return a pair of functions, the first being the random check, and the second a function that will reset the internal state back to the starting values. If something goes wrong an exception will be thrown.
Random values are assumed to be in the same range as the rand builtin accepts, or
rand
0.0 <= r < 1.0
Increases the starting odds by step-odds each miss, resets to reset or start odds on hit. Optional keys rand (custom random function, should use same return range as builtin rand does) and reset (odds to reset to, if different from the start odds).
(Players may quickly realize and become annoyed by things that always miss on the first try, so maybe only make start slightly lower than the reset value when imposing a first use penalty. On the other hand, beginner's luck can be a thing.)
Increases the odds by the value given in table at the rotating index index on miss; resets to reset or start on hit, and also resets the index to 0 on hit. Optional keys index (custom starting index, default is 0) and as for the previous function rand and reset.
0
Unlike prd_step the table allows for non-linear odds changes, and will wrap around back to the beginning instead of always (eventually) adding up to a value greater than 100% (assuming a positive step value); this means prd_table can permit longer runs of misses, depending on the table used.
The table is copied so cannot be fiddled with afterwards (not directly nor easily, anyways).
prd_step( start => 0.25, step => 0.41675 )
was above claimed without evidence to be (roughly) a coinflip; what this tests for is whether a random value is below 0.25, 0.66, and then 1.08. This means the distribution will be quite a bit different from the output of a random function:
$ cd eg $ ./trial | ./runstats | r-fu table 50.0351 1 2 3 125308 250439 124604
So no more than three flips without the result changing to a different value; an unbiased roll instead may show (increasingly rare but not absent) runs of 10, 20, or even more flips without a change in value:
$ perl -E'say rand()>0.5?1:0 for 1..1e6'|./runstats|r-fu table 1 2 3 4 5 6 7 8 9 250244 124873 62288 31391 15578 7803 3954 1910 1018 10 11 12 13 14 15 16 17 18 468 252 136 54 34 18 6 3 2
The main point of this module is to reduce this variance, but probably not so far as done for the example coinflip, and most likely in the context of a game where too much variance is bad (luck is too much of a factor).
Please report any bugs or feature requests to bug-game-pseudorand at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Game-PseudoRand.
bug-game-pseudorand at rt.cpan.org
Patches might best be applied towards:
https://github.com/thrig/Game-PseudoRand
https://github.com/thrig/ministry-of-silly-vaults/ contains a discussion and example code under the pseudo-random-dist directory.
pseudo-random-dist
Various alternative random modules to the (bad, but fast) builtin rand call exist on CPAN, e.g. Math::Random::PCG32, among others.
https://github.com/thrig/r-fu/ is the command line bridge to R used in the shell statistics commands, above.
thrig - Jeremy Mates (cpan:JMATES) <jmates at cpan.org>
<jmates at cpan.org>
Copyright (C) 2019 by Jeremy Mates
This program is distributed under the (Revised) BSD License: http://www.opensource.org/licenses/BSD-3-Clause
To install Game::PseudoRand, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Game::PseudoRand
CPAN shell
perl -MCPAN -e shell install Game::PseudoRand
For more information on module installation, please visit the detailed CPAN module installation guide.