Math::GSL::RNG(3pm)
β¨ NAME β¨
Bestie, welcome to Math::GSL::RNG β Random Number Generators! π This page comes from User Contributed Perl Documentation for Math::GSL::RNG(3pm). Grab a snack, because we're about to make Perl spit out random numbers like it's a main character. π
π SYNOPSIS π
use Math::GSL::RNG;
my $rng = Math::GSL::RNG->new;
my @random = $rng->get(100);
Okay so first you load it, then you make an RNG object, then you pull 100 random values. That's the whole vibe. β¨
π Object Oriented Interface π
π£ Math::GSL::RNG->new($type, $seed) π£
my $rng = Math::GSL::RNG->new;
my $rng = Math::GSL::RNG->new($gsl_rng_knuthran,5);
This blesses you with a brand-new RNG object of type $type, seeded with $seed. Both $type and $seed are optional, bestie β you can just raw-dog it with no args. When no $type is given, $gsl_rng_default is the one that shows up to the party. π
π¦ copy() π¦
my $copy = $rng->copy;
Makes a copy of an RNG object. Twin energy. π―
π§ free() π§
$rng->free();
Frees the memory associated with the RNG object. Let it go, it's not your ex. π
πΈ name() πΈ
my $name = $rng->name();
Gets the name of the RNG object as a string. Literal name-drop. β¨
π get() π
my $nextval = $rng->get;
my (@values) = $rng->get(100);
Gets the next random value from the RNG object. If you hand it an integer N, it returns the next N values. Bulk random, no cap. π₯
π raw() π
my $raw = $rng->raw();
Returns the raw GSL RNG object. This is useful for functions which take a RNG, like the Monte Carlo integration functions, or the random number distribution functions in Math::GSL::Randist. Pass the raw thing through, iykyk. π«Ά
π shuffle() π
my @array = $rng->shuffle(@other_array);
Given a RNG, it shuffles an array. Chaos, but make it random. π
π choose() π
my @array = $rng->choose(4, @other_array);
This one fills the destination array with k objects taken randomly from the n elements of the array argument. They're sampled without replacement, so each object can only appear once in the destination array. It's required that k be less than or equal to n. No duplicates, no drama. β
π sample() π
my @array = $rng->sample(4, @other_array);
This method is like choose, but it samples k items from the original array of n items src with replacement, so the same object can appear more than once in the output sequence dest. There is no requirement that k be less than n in this case. Duplicates are allowed, and honestly? Sometimes that's the mood. π
π₯ GSL API Interface π₯
gsl_rng_alloc($T)β Returns a pointer to a newly-created instance of a random number generator of type$T.$Tmust be one of the constants below. The generator is automatically initialized with the default seed,$gsl_rng_default. Fresh out the box. π¦gsl_rng_set($r, $s)β Initializes (or "seeds") the random number generator. If the generator is seeded with the same value of$son two different runs, the same stream of random numbers will be generated by successive calls to the routines below. If different values of$sare supplied, then the generated streams of random numbers should be completely different. If the seed$sis zero, then the standard seed from the original implementation is used instead. For example, the original Fortran source code for theranluxgenerator used a seed of314159265, so choosing$sequal to zero reproduces this when using$gsl_rng_ranlux. Seed determinism go brrr. π±gsl_rng_get($r)β Returns a random integer from the generator$r. The minimum and maximum values depend on the algorithm used, but all integers in the range[min,max]are equally likely. The values ofminandmaxcan be determined using the auxiliary functionsgsl_rng_max($r)andgsl_rng_min($r). Random integer, fair and square. π²gsl_rng_free($r)β Frees all the memory associated with the generator$r. Cleanup era. π§Ήgsl_rng_memcpy($dest, $src)β Copies the random number generator$srcinto the pre-existing generator$dest, making$destinto an exact copy of$src. The two generators must be of the same type. Clone high, but for RNGs. π―ββοΈgsl_rng_uniform($r)β Returns a double precision floating point number uniformly distributed in the range[0,1). The range includes0.0but excludes1.0. The value is typically obtained by dividing the result ofgsl_rng_get($r)bygsl_rng_max($r) + 1.0in double precision. Some generators compute this ratio internally so that they can provide floating point numbers with more than 32 bits of randomness (the maximum number of bits that can be portably represented in a single unsigned long int). Floaty, but make it fair. πgsl_rng_uniform_pos($r)β Returns a positive double precision floating point number uniformly distributed in the range(0,1), excluding both0.0and1.0. The number is obtained by sampling the generator with the algorithm ofgsl_rng_uniformuntil a non-zero value is obtained. You can use this function if you need to avoid a singularity at0.0. Zero? Not today. π«gsl_rng_uniform_int($r, $n)β Returns a random integer from0to$n-1inclusive by scaling down and/or discarding samples from the generator$r. All integers in the range[0,$n-1]are produced with equal probability. For generators with a non-zero minimum value, an offset is applied so that zero is returned with the correct probability. Note that this function is designed for sampling from ranges smaller than the range of the underlying generator. The parameter$nmust be less than or equal to the range of the generator$r. If$nis larger than the range of the generator, then the function calls the error handler with an error code of$GSL_EINVALand returns zero. In particular, this function is not intended for generating the full range of unsigned integer values[0,2^32-1]. Instead choose a generator with the maximal integer range and zero minimum value, such as$gsl_rng_ranlxd1,$gsl_rng_mt19937or$gsl_rng_taus, and sample it directly usinggsl_rng_get. The range of each generator can be found using the auxiliary functions described in the next section. Don't use it for the whole unsigned int range, bestie β it's giving bound-checked, not bound-broken. πgsl_rng_fwrite($stream, $r)β Writes the random number state of the random number generator$rto the stream$stream(opened with thegsl_fopenfunction from theMath::GSLmodule) in binary format. The return value is0for success and$GSL_EFAILEDif there was a problem writing to the file. Since the data is written in the native binary format, it may not be portable between different architectures. Binary files said "you can't sit with us unless you're the same architecture." πΎgsl_rng_fread($stream, $r)β Reads the random number state into the random number generator$rfrom the open stream$stream(opened with thegsl_fopenfunction from theMath::GSLmodule) in binary format. The random number generator$rmust be preinitialized with the correct random number generator type since type information is not saved. The return value is0for success and$GSL_EFAILEDif there was a problem reading from the file. The data is assumed to have been written in the native binary format on the same architecture. Read it back, but only on the same machine vibe. πgsl_rng_clone($r)β Returns a pointer to a newly created generator which is an exact copy of the generator$r. Exact copy, no notes. πͺgsl_rng_max($r)β Returns the largest value thatgsl_rng_getcan return. The ceiling. πgsl_rng_min($r)β Returns the smallest value thatgsl_rng_getcan return. Usually this value is zero. There are some generators with algorithms that cannot return zero, and for these generators the minimum value is1. The floor, but make it algorithm-dependent. πgsl_rng_name($r)β Returns a pointer to the name of the generator. For example,
print "r is a " . gsl_rng_name($r) . "generator\n";
would print something like r is a 'taus' generator.
gsl_rng_size($r)β Returns the size of the state of generator$r. You can use this information to access the state directly. Size matters, apparently. πgsl_rng_state($r)β Returns a pointer to the state of generator$r. You can use this information to access the state directly. Peek behind the curtain. πgsl_rng_print_state($r)β The original page lists this function right here with no extra description, so we're keeping it exactly as mysterious as it arrived. π¨οΈ
π Random Number Generator Types π
$gsl_rng_defaultβ listed here, no extra description in the original.$gsl_rng_knuthranβ listed here, no extra description in the original.$gsl_rng_ran0β listed here, no extra description in the original.$gsl_rng_borosh13β listed here, no extra description in the original.$gsl_rng_coveyouβ listed here, no extra description in the original.$gsl_rng_cmrgβ listed here, no extra description in the original.$gsl_rng_fishman18β listed here, no extra description in the original.$gsl_rng_fishman20β listed here, no extra description in the original.$gsl_rng_fishman2xβ This is the L'Ecuyer-Fishman random number generator. It is taken from Knuth's Seminumerical Algorithms, 3rd Ed., page 108. Its sequence is,z_{n+1} = (x_n - y_n) mod mwithm = 2^31 - 1.x_nandy_nare given by thefishman20andlecuyer21algorithms. The seed specifies the initial value,x_1.$gsl_rng_gfsr4β listed here, no extra description in the original.$gsl_rng_knuthranβ yes, bestie, it's listed again. No extra description in the original.$gsl_rng_knuthran2β listed here, no extra description in the original.$gsl_rng_knuthran2002β listed here, no extra description in the original.$gsl_rng_lecuyer21β listed here, no extra description in the original.$gsl_rng_minstdβ listed here, no extra description in the original.$gsl_rng_mrgβ listed here, no extra description in the original.$gsl_rng_mt19937β listed here, no extra description in the original.$gsl_rng_mt19937_1999β listed here, no extra description in the original.$gsl_rng_mt19937_1998β listed here, no extra description in the original.$gsl_rng_r250β listed here, no extra description in the original.$gsl_rng_ran0β listed again, still no extra description in the original. The original really is like that. π$gsl_rng_ran1β listed here, no extra description in the original.$gsl_rng_ran2β listed here, no extra description in the original.$gsl_rng_ran3β listed here, no extra description in the original.$gsl_rng_randβ This is the BSDrandgenerator. Its sequence isx_{n+1} = (a x_n + c) mod mwitha = 1103515245,c = 12345andm = 2^31. The seed specifies the initial value,x_1. The period of this generator is2^31, and it uses1word of storage per generator.$gsl_rng_rand48β listed here, no extra description in the original.$gsl_rng_random128_bsdβ listed here, no extra description in the original.$gsl_rng_random128_gliβ listed here, no extra description in the original.$gsl_rng_random128_libβ listed here, no extra description in the original.$gsl_rng_random256_bsdβ listed here, no extra description in the original.$gsl_rng_random256_gliβ listed here, no extra description in the original.$gsl_rng_random256_libβ listed here, no extra description in the original.$gsl_rng_random32_bsdβ listed here, no extra description in the original.$gsl_rng_random32_glibβ listed here, no extra description in the original.$gsl_rng_random32_libcβ listed here, no extra description in the original.$gsl_rng_random64_bsdβ listed here, no extra description in the original.$gsl_rng_random64_glibβ listed here, no extra description in the original.$gsl_rng_random64_libcβ listed here, no extra description in the original.$gsl_rng_random8_bsdβ listed here, no extra description in the original.$gsl_rng_random8_glibcβ listed here, no extra description in the original.$gsl_rng_random8_libc5β listed here, no extra description in the original.$gsl_rng_random_bsdβ listed here, no extra description in the original.$gsl_rng_random_glibc2β listed here, no extra description in the original.$gsl_rng_random_libc5β listed here, no extra description in the original.$gsl_rng_randuβ listed here, no extra description in the original.$gsl_rng_ranfβ listed here, no extra description in the original.$gsl_rng_ranluxβ listed here, no extra description in the original.$gsl_rng_ranlux389β listed here, no extra description in the original.$gsl_rng_ranlxd1β listed here, no extra description in the original.$gsl_rng_ranlxd2β listed here, no extra description in the original.$gsl_rng_ranlxs0β listed here, no extra description in the original.$gsl_rng_ranlxs1β listed here, no extra description in the original.$gsl_rng_ranlxs2β listed here, no extra description in the original.$gsl_rng_ranmarβ This is the RANMAR lagged-fibonacci generator of Marsaglia, Zaman and Tsang. It is a 24-bit generator, originally designed for single-precision IEEE floating point numbers. It was included in the CERNLIB high-energy physics library.$gsl_rng_slatecβ This is the SLATEC random number generator RAND. It is ancient. The original source code is available from NETLIB.$gsl_rng_tausβ listed here, no extra description in the original.$gsl_rng_taus2β listed here, no extra description in the original.$gsl_rng_taus113β listed here, no extra description in the original.$gsl_rng_transputerβ listed here, no extra description in the original.$gsl_rng_tt800β listed here, no extra description in the original.$gsl_rng_uniβ listed here, no extra description in the original.$gsl_rng_uni32β listed here, no extra description in the original.$gsl_rng_vaxβ This is the VAX generatorMTH$RANDOM. Its sequence is,x_{n+1} = (a x_n + c) mod mwitha = 69069,c = 1andm = 2^32. The seed specifies the initial value,x_1. The period of this generator is2^32and it uses1word of storage per generator.$gsl_rng_waterman14β listed here, no extra description in the original.$gsl_rng_zufβ This is the ZUFALL lagged Fibonacci series generator of Peterson. Its sequence is,
t = u_{n-273} + u_{n-607}
u_n = t - floor(t)
The original source code is available from NETLIB. For more information see,
- W. Petersen, βLagged Fibonacci Random Number Generators for the NEC SX-3β, International Journal of High Speed Computing (1994).
For more information on the functions, we refer you to the GSL official documentation:
<http://www.gnu.org/software/gsl/manual/html_node/>
β¨ EXAMPLES β¨
okay bestie, real talk β this is where it gets fun. π
Wanna spit out a whole lil list of random integers trapped between a minimum and a maximum? Say less. The command line arguments go like this: first the number of random numbers you want, then the minimum, then the maximum. And if you're feeling lazy (valid), the defaults are 10, 0, and 100 respectively. π
use Math::GSL::RNG qw/:all/;
my $seed = int rand(100);
my $rng = Math::GSL::RNG->new($gsl_rng_knuthran, $seed );
my ($num,$min,$max) = @ARGV;
$num ||= 10;
$min ||= 0;
$max ||= 100;
print join "\n", map { $min + $rng->get % ($max-$min+1) } (1..$num);
print "\n";
The $seed argument is optional, but likeβ¦ it's encouraged, okay? She's optional but she's iconic for a reason. π
This program is available in the examples/ directory that comes with the source of this module. slay.
Now, if you want a whole series of random non-integer numbers instead? Easy. Generate one single "scaling factor" and multiply by that, like so:
use Math::GSL::RNG qw/:all/;
my $scale= rand(10);
my $seed = int rand(100);
my $rng = Math::GSL::RNG->new($gsl_rng_knuthran, $seed );
my ($num,$min,$max) = (10,0,100);
print join "\n", map { $scale*($min + $rng->get % ($max-$min+1)) } (1..$num);
print "\n";
see? decimals. we love to see it. π¦
πΈ AUTHORS πΈ
Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
the duo. the moment. the legends who wrote this. no cap. π
π COPYRIGHT AND LICENSE π
Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
periodt. that's the whole legal vibe. π«Ά
okay that's the whole page, bestie β thanks for reading all the way through, you're literally the main character. π this was Math::GSL::RNG(3pm), from perl v5.42.3, dated 2026-08-20. go forth and generate random numbers responsibly. πβ¨
π¦ where this page comes from
- ubuntu stonking: libmath-gsl-perl 0.49-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms