Math::...::Range(3pm)
πΈ NAME πΈ
Math::Random::MT::Auto::Range β range-valued PRNGs, bestie π
okay so real talk: this page is Math::...::Range(3pm) from the User Contributed Perl Documentation, and it's giving tiny module, massive utility energy. It makes random number generators that live inside a range. Periodt. β¨
π SYNOPSIS π
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();
likeβ¦ a die that rolls itself and a compass that picks its own course?? main character behavior π
π¦ DESCRIPTION π¦
This module builds range-valued pseudorandom number generators (PRNGs) β little objects that hand you random values between two limits you specify. Simple. Iconic. No notes. π
And while that's genuinely useful on its own, the primary purpose of this module is to be a whole example: it shows you how to create subclasses of Math::Random::MT::Auto within Object::InsideOut's inside-out object model. So it's a working module AND a tutorial bestie. We love a multitasker π«Ά
π MODULE DECLARATION π
Slap this at the top of your application code:
use strict;
use warnings;
use Math::Random::MT::Auto::Range;
Heads up: this module is strictly OO. It does not export any functions or symbols. No imports, no shortcuts, just vibes and objects π
π METHODS π
Math::Random::MT::Auto::Range->new
Creates a brand new range-valued PRNG. Baby's first PRNG πΌβ¨
my $prng = Math::Random::MT::Auto::Range->new( %options );
The options it takes are:
'LOW' => $numand'HIGH' => $numβ these set the limits that the values returned by the PRNG will range over. They're mandatory (you cannot skip them, bestie) andLOWmust not be equal toHIGH. If theTYPEfor the PRNG isINTEGER, the range isLOWtoHIGHinclusive (i.e.,[LOW, HIGH]). If it'sDOUBLE, then it'sLOWinclusive toHIGHexclusive (i.e.,[LOW, HIGH)). Oh and plot twist:LOandHIwork as synonyms forLOWandHIGH, respectively π'TYPE' => 'INTEGER'and'TYPE' => 'DOUBLE'β sets the type of the values that come out of the PRNG. If you don't specifyTYPE, it defaults toINTEGERwhen bothLOWandHIGHare integers. Fancy, right? π
The options above are also supported in lowercase and mixed-case (like 'low', 'hi', 'Type', etc.) β so type it however feels right in your heart π
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. (yes, it's listed twice, that's literally what the doc says, no cap π)
$obj->new
Makes a new PRNG exactly the same way Math::Random::MT::Auto::Range->new does:
my $prng2 = $prng1->new( %options );
And on top of the methods described below, objects made by this package inherit all the object methods the Math::Random::MT::Auto class provides β including the clone() method. Free stuff, we love to see it π
$obj->rrand
Returns a random number of the configured type, inside the configured range. The whole point of the module, honestly β¨
my $rand = $prng->rrand();
Same rules as before: if the PRNG's TYPE is INTEGER, the range is LOW to HIGH inclusive (i.e., [LOW, HIGH]). If DOUBLE, it's LOW inclusive to HIGH exclusive (i.e., [LOW, HIGH)). π
$obj->set_range_type
Sets the numeric type for the random numbers the PRNG hands back:
$prng->set_range_type('INTEGER');
# or
$prng->set_range_type('DOUBLE');
$obj->get_range_type
Tells you the numeric type (INTEGER or DOUBLE) of the random numbers your PRNG returns:
my $type = $prng->get_range_type();
$obj->set_range
Sets the limits for the PRNG's return-value range:
$prng->set_range($lo, $hi);
$lo must not be equal to $hi. Zero-width ranges are not it π
ββοΈ
$obj->get_range
Returns a list of the PRNG's range limits:
my ($lo, $hi) = $prng->get_range();
π§ INSIDE-OUT OBJECTS π§
All the capabilities that Object::InsideOut provides are supported by this module. For more info, go look at "INSIDE-OUT OBJECTS" in Math::Random::MT::Auto π
Coercion
Object coercion is supported in exactly the same manner as documented in "Coercion" in Math::Random::MT::Auto β with one little difference: the underlying random number method is ->rrand(). That's the whole twist π
π DIAGNOSTICS π
- Missing parameter: LOW
- Missing parameter: HIGH β the LOW and HIGH values for the range must be specified to
->new(). You can't build a range with no edges, bestie π - Arg to
->set_range_type()must be 'INTEGER' or 'DOUBLE' β self explanatory π ->range()requires two numeric args β self explanatory π- Invalid arguments: LOW and HIGH are equal β you cannot specify a range of zero width. That's just a single point. Not a range. We don't do that here π
Good news though! This module will reverse the range limits if you specify them in the wrong order β i.e., it makes sure that LOW < HIGH for you. Your chaos, auto-corrected π«Ά
β SEE ALSO β
Math::Random::MT::AutoObject::InsideOut
π AUTHOR π
Jerry D. Hedden, <jdhedden AT cpan DOT org> β legend behavior, fr fr π
π COPYRIGHT AND LICENSE π
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.
and that's the whole page, bestie π this was perl v5.42.3, dated 2026-08-20 β thanks for reading my lilMath::...::Range(3pm) breakdown, now go generate some random numbers in a range πβ¨π¦
π¦ where this page comes from
- ubuntu stonking: libmath-random-mt-auto-perl 6.23-2build5 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms