Math::...ormInt(3pm)

πŸ“¦
version 0.22 Β· 2 versions
  • 0.22 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.22 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Math::Random::OO::UniformInt πŸ’–

okay bestie, so this is a page from the User Contributed Perl Documentation manual β€” it's Math::...ormInt(3pm) if you're squinting at the terminal header β€” and it's literally just about making random whole numbers that are fair. every number gets the same chance. no favorites. egalitarian queen behavior. πŸ‘‘


✨ NAME ✨

Math::Random::OO::UniformInt β€” generates random integers with uniform probability


🌸 VERSION 🌸

version 0.22


πŸŽ€ SYNOPSIS πŸŽ€

okay so here's the vibe, copy-paste ready:

use Math::Random::OO::UniformInt;
push @prngs,
    Math::Random::OO::UniformInt->new(),     # 0 or 1
    Math::Random::OO::UniformInt->new(5),    # 0, 1, 2, 3, 4, or 5
    Math::Random::OO::UniformInt->new(-1,1); # -1, 0, or 1
$_->seed(42) for @prngs;
print( $_->next() . "\n" ) for @prngs;

see how the comments tell you exactly what range each one spits out? 🫢 iconic. seed them all with 42, then print one number from each. boom.


πŸ¦‹ DESCRIPTION πŸ¦‹

so this is a subclass of Math::Random::OO, and what it does is generate random integers with uniform probability. meaning: every single integer in the range has exactly the same odds. no number is the main character. they're all main characters. periodt.


πŸ’… METHODS πŸ’…

⭐ "new" ⭐

$prng1 = Math::Random::OO::UniformInt->new();
$prng2 = Math::Random::OO::UniformInt->new($high);
$prng3 = Math::Random::OO::UniformInt->new($low,$high);

okay so "new" takes up to two optional parameters and hands you back a fresh new "Math::Random::OO::UniformInt" object. ✨

and here's the thing that makes it different from Uniform: this one gives you integers, and it's inclusive of both endpoints. both of them. the low one AND the high one. not like those half-open-interval girlies. πŸ’€

  • no parameters β†’ the object generates random integers from zero (inclusive) to one (inclusive).
  • one parameter β†’ it generates random numbers from zero (inclusive) to the value of the parameter (inclusive).
⚠️ okay listen, note this: the object does this with multiplication, so if the parameter is negative, the function will return negative numbers. this is a feature or bug, depending on your point of view. i'm not judging. i'm just reporting. πŸ™ˆ
  • two parameters β†’ it generates random integers from the first parameter (inclusive) to the second parameter (inclusive). and actually, as long as you have two parameters, "new" will put them in the right order. so it's got your back, bestie. 🫢

and if your parameters are non-integers? they get truncated to integers before the range is calculated. so "new(-1.2, 3.6)" is equivalent to "new(-1,3)". slay, no cap.


⭐ "seed" ⭐

$rv = $prng->seed( @seeds );

this method seeds the random number generator. 🌱

at the moment, only the first seed value matters. it should be a positive integer.


⭐ "next" ⭐

$rnd = $prng->next();

this method returns the next random number from the random number generator. that's it. that's the whole job. it does not take any parameters. no notes. πŸ’…


πŸ‘‘ AUTHOR πŸ‘‘

David Golden <dagolden@cpan.org>


This software is Copyright (c) 2013 by David Golden.

This is free software, licensed under:

The Apache License, Version 2.0, January 2004

okay that's the whole page, bestie!! πŸŽ€ go forth and generate fair random integers, you absolute legend. ✨

this was perl v5.36.0, dated 2022-12-12, from the Math::...ormInt(3pm) corner of the manual β€” love that for us. byeee! πŸ’–πŸŒΈπŸ¦‹