NAME
Math::Random::MT::Auto::Range - Range-valued PRNGs
SYNOPSIS
use strict;
use warnings;
use Math::Random::MT::Auto::Range;
# Integer random number range
my $die = Math::Random::MT::Auto::Range(LO => 1, HI => 6);
my $roll = $die->rrand();
# Floating-point random number range
my $compass = Math::Random::MT::Auto::Range(LO => 0, HI => 360,
TYPE => 'DOUBLE');
my $course = $compass->rrand();
DESCRIPTION
This module creates range-valued pseudo-random 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 the inside-out object model.
USAGE
- Module Declaration
-
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.
- Math::Random::MT::Auto::Range->new
-
Creates a new range-valued PRNG.
my $prng = Math::Random::MT::Auto::Range->new( %options );
Available options are:
- 'LOW' => $num
- 'HIGH' => $num
-
Sets the limits over which the values return by the PRNG will range. If the
TYPE
for the PRNG isINTEGER
, then the range will beLOW
toHIGH
inclusive (i.e., [LOW, HIGH]). IfDOUBLE
, thenLOW
inclusive toHIGH
exclusive (i.e., [LOW, HIGH)).LOW
andHIGH
must be specified when callingnew
as a class method.LO
andHI
can be used as synonyms forLOW
andHIGH
, respectively. - 'TYPE' => 'INTEGER'
- 'TYPE' => 'DOUBLE'
-
Sets the type for the values returned from the PRNG. If
TYPE
is not specified, it will default toINTEGER
if bothLOW
andHIGH
are integers.
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
andSTATE
. - $obj->new
-
Creates a new PRNG, using any specified options, and then using attributes from the referenced PRNG.
my $prng2 = $prng1->new( %options );
With no options, the new PRNG will be a complete clone of the referenced PRNG.
- $obj->set_range_type
-
Sets the numeric type for the random numbers returned by the PRNG.
$prng->set_range_type('INTEGER'); # or $prng->set_range_type('DOUBLE');
- $obj->get_range_type
-
Returns the numeric type ('INTEGER' or 'DOUBLE') for the random numbers returned by the PRNG.
my $type = $prng->get_range_type();
- $obj->set_range
-
Sets the limits for the PRNG's return value range.
$prng->set_range($lo, $hi);
- $obj->get_range
-
Returns a list of the PRNG's range limits.
my ($lo, $hi) = $prng->get_range();
- $obj->rrand
-
Returns a random number of the configured type within the configured range.
my $rand = $prng->rrand();
If the
TYPE
for the PRNG isINTEGER
, then the range will beLOW
toHIGH
inclusive (i.e., [LOW, HIGH]). IfDOUBLE
, thenLOW
inclusive toHIGH
exclusive (i.e., [LOW, HIGH)).
In addition to the methods describe above, the objects created by this package also support all the object methods provided by the Math::Random::MT::Auto class.
SEE ALSO
AUTHOR
Jerry D. Hedden, <jdhedden AT 1979 DOT usna DOT com>
COPYRIGHT AND LICENSE
Copyright 2005 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.