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' => $num and 'HIGH' => $num β€” these set the limits that the values returned by the PRNG will range over. They're mandatory (you cannot skip them, bestie) and LOW must not be equal to HIGH. If the TYPE for the PRNG is INTEGER, the range is LOW to HIGH inclusive (i.e., [LOW, HIGH]). If it's DOUBLE, then it's LOW inclusive to HIGH exclusive (i.e., [LOW, HIGH)). Oh and plot twist: LO and HI work as synonyms for LOW and HIGH, respectively πŸŽ€
  • 'TYPE' => 'INTEGER' and 'TYPE' => 'DOUBLE' β€” sets the type of the values that come out of the PRNG. If you don't specify TYPE, it defaults to INTEGER when both LOW and HIGH are 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::Auto
  • Object::InsideOut

πŸ‘‘ AUTHOR πŸ‘‘

Jerry D. Hedden, <jdhedden AT cpan DOT org> β€” legend behavior, fr fr 🌟


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