Math::Random::OO(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 πŸ’–

okay bestie, so this is the manual page for Math::Random::OO(3pm) and it's literally the blueprint for making random numbers behave themselves. sit down, hydrate, let's go 🫢

version 0.22 ✨ (yes that's the whole vibe)


✨ SYNOPSIS ✨

# Using factory functions
use Math::Random::OO qw( Uniform UniformInt );
push @prngs, Uniform(), UniformInt(1,6);

# Explicit creation of subclasses
use Math::Random::OO::Normal;
push @prngs, Math::Random::OO::Normal->new(0,2);

$_->seed(23) for (@prngs);
print( $_->next(), "\n") for (@prngs);

like. look at that. four lines and you've got a whole squad of generators. iconic. πŸ‘‘


🌈 DESCRIPTION 🌈

so CPAN has SO many modules for generating random numbers β€” different ways, different probability distributions, some doing pseudo-random number generation algorithms, some pulling from other entropy sources. (the SEE ALSO section below has a lil sampler platter if you wanna peek.) πŸ™ˆ

but here's the thing β€” there's no standard interface across any of them. no cap. every module does its own thing. πŸ’€

so this module defines an abstract interface for random number generation. subclasses of this model will either implement specific types of random number generators, or they'll wrap existing random number generators. either way: consistency. ✨

and yes, this consistency comes at the cost of some efficiency, but it's so worth it because it means you can write generic routines that manipulate any random number generator adhering to the interface. like β€” picture a stochastic simulation that takes a bunch of user-supplied parameters, and each one is a Math::Random::OO subclass object representing a stochastic variable with its own particular probability distribution. that's the dream. that's the vision. πŸ¦‹


πŸŽ€ USAGE πŸŽ€

🧁 Factory Functions

use Math::Random::OO qw( Uniform UniformInt Normal Bootstrap );
$uniform = Uniform(-1,1);
$uni_int = UniformInt(1,6);
$normal  = Normal(1,1);
$boot    = Bootstrap( 2, 3, 3, 4, 4, 4, 5, 5, 5 );

so on top of defining that abstract interface for subclasses, this module also imports subclasses and exports factory functions when you ask for them β€” so you can spin up a whole crowd of random number generators at once without typing Math::Random::OO::Subclass->new() over and over like some kind of medieval scribe. 😭

the factory function names are the same as the suffix of the subclass that follows Math::Random::OO. when you call one, it passes its arguments directly to the new constructor method of the corresponding subclass and hands you back a fresh new object of that subclass type. chef's kiss.

here's what's supported, and the subclass each one is giving:

  • Uniform β€” Math::Random::OO::Uniform (uniform distribution over a range) πŸ“
  • UniformInt β€” Math::Random::OO::UniformInt (uniform distribution of integers over a range) 🎲
  • Normal β€” Math::Random::OO::Normal (normal distribution with specified mean and standard deviation) πŸ””
  • Bootstrap β€” Math::Random::OO::Bootstrap (bootstrap resampling from a non-parameteric distribution) πŸ‘’

πŸ‘‘ INTERFACE πŸ‘‘

every single Math::Random::OO subclass must follow a standard interface. non-negotiable bestie. they must provide a new method, a seed method, and a next method. the specific details of each are left up to the individual subclass β€” that part's their business. πŸ’…

🌸 new

this is the standard constructor. each subclass will define parameters specific to that subclass. that's it, that's the whole deal. 🫢

🌸 seed

$prng->seed( @seeds );

this method takes a seed (or a whole list of seeds) and uses it to set the initial state of the random number generator. and because some subclasses may optionally use or require a list of seeds, the interface mandates that a list must be acceptable. so if your generator only wants a single seed, it must use the first value in the list. no exceptions. periodt.

and heads up β€” since seeds may be passed to the built-in srand() function, they may be truncated as integers, so 0.12 and 0.34 would be the same seed. 😭 which means: only positive integers should be used. don't be out here seeding with vibes and decimals, it will not go how you think.

🌸 next

$rnd = $prng->next();

this method returns the next random number from the random number generator. it does not take any parameters, and it must not use any parameters either. it's giving zero-argument main character energy. ⭐


πŸ”₯ SEE ALSO πŸ”₯

okay so this is not an exhaustive list β€” search CPAN if that's what you're after β€” but it represents some of the more common or established random number generators that the author has come across. iykyk.

  • Math::Random β€” multiple random number generators for different distributions (a port of the C randlib)
  • Math::Rand48 β€” perl bindings for the drand48 library (according to perl56delta, this may already be the default after perl 5.005_52 if available)
  • Math::Random::MT β€” The Mersenne Twister PRNG (good and fast)
  • Math::TrulyRandom β€” an interface to random numbers from interrupt timing discrepancies

πŸ¦‹ SUPPORT πŸ¦‹

πŸ› Bugs / Feature Requests

please report any bugs or feature requests through the issue tracker at <https://github.com/dagolden/math-random-oo/issues>. you'll be notified automatically of any progress on your issue, so you're never left on read. πŸ’œ

🧚 Source Code

this is open source software. the code repository is available for public review and contribution under the terms of the license. 🫢

<https://github.com/dagolden/math-random-oo>

git clone git://github.com/dagolden/math-random-oo.git

⭐ 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, you ate that πŸ“πŸ’– and just so you know who to thank for the docs as rendered: this was perl v5.36.0, dated 2022-12-12, filed under Math::Random::OO(3pm) β€” mwah, go generate something chaotic. βœ¨πŸŽ€πŸ¦‹