Math::Random::MT::Auto::Range(3pm) | User Contributed Perl Documentation | Math::Random::MT::Auto::Range(3pm) |
Math::Random::MT::Auto::Range - Range-valued PRNGs
use strict; use warnings; use Math::Random::MT::Auto::Range; # Integer random number range my $die = Math::Random::MT::Auto::Range->new(LO => 1, HI => 6); my $roll = $die->rrand(); # Floating-point random number range my $compass = Math::Random::MT::Auto::Range->new(LO => 0, HI => 360, TYPE => 'DOUBLE'); my $course = $compass->rrand();
This module creates range-valued pseudorandom number generators (PRNGs) that return random values between two specified limits.
While useful in itself, the primary purpose of this module is to provide an example of how to create subclasses of Math::Random::MT::Auto within Object::InsideOut's inside-out object model.
Add the following to the top of our application code:
use strict; use warnings; use Math::Random::MT::Auto::Range;
This module is strictly OO, and does not export any functions or symbols.
my $prng = Math::Random::MT::Auto::Range->new( %options );
Available options are:
If the "TYPE" for the PRNG is "INTEGER", then the range will be "LOW" to "HIGH" inclusive (i.e., [LOW, HIGH]). If "DOUBLE", then "LOW" inclusive to "HIGH" exclusive (i.e., [LOW, HIGH)).
"LO" and "HI" can be used as synonyms for "LOW" and "HIGH", respectively.
The options above are also supported using lowercase and mixed-case (e.g., 'low', 'hi', 'Type', etc.).
Additionally, objects created with this package can take any of the options supported by the Math::Random::MT::Auto class, namely, "STATE", "SEED" and "STATE".
my $prng2 = $prng1->new( %options );
In addition to the methods describe below, the objects created by this package inherit all the object methods provided by the Math::Random::MT::Auto class, including the "-"clone()> method.
my $rand = $prng->rrand();
If the "TYPE" for the PRNG is "INTEGER", then the range will be "LOW" to "HIGH" inclusive (i.e., [LOW, HIGH]). If "DOUBLE", then "LOW" inclusive to "HIGH" exclusive (i.e., [LOW, HIGH)).
$prng->set_range_type('INTEGER'); # or $prng->set_range_type('DOUBLE');
my $type = $prng->get_range_type();
$prng->set_range($lo, $hi);
$lo must not be equal to $hi.
my ($lo, $hi) = $prng->get_range();
Capabilities provided by Object::InsideOut are supported by this modules. See "INSIDE-OUT OBJECTS" in Math::Random::MT::Auto for more information.
Object coercion is supported in the same manner as documented in See "Coercion" in Math::Random::MT::Auto except that the underlying random number method is "->rrand()".
The LOW and HIGH values for the range must be specified to ->new().
Self explanatory.
Self explanatory.
You cannot specify a range of zero width.
This module will reverse the range limits if they are specified in the wrong order (i.e., it makes sure that "LOW < HIGH").
Math::Random::MT::Auto
Object::InsideOut
Jerry D. Hedden, <jdhedden AT cpan DOT org>
Copyright 2005 - 2009 Jerry D. Hedden. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2023-02-07 | perl v5.36.0 |