Scroll to navigation

Math::GSL::RNG(3pm) User Contributed Perl Documentation Math::GSL::RNG(3pm)

NAME

Math::GSL::RNG - Random Number Generators

SYNOPSIS

    use Math::GSL::RNG;
    my $rng     = Math::GSL::RNG->new;
    my @random  = $rng->get(100);

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);

Creates a new RNG object of type $type, seeded with $seed. Both of these parameters are optional. The type $gsl_rng_default is used when no $type is given.

copy()

    my $copy = $rng->copy;

Make a copy of a RNG object.

free()

    $rng->free();

Free memory associated with RNG object.

name()

   my $name = $rng->name();

Get the name of the RNG object as a string.

get()

    my $nextval  = $rng->get;
    my (@values) = $rng->get(100);

Get the next random value from the RNG object. If given an integer N, returns the next N values.

raw()

    my $raw = $rng->raw();

Return the raw GSL RNG object, useful for functions which take a RNG, such as the Monte Carlo integration functions or the random number distribution functions in Math::GSL::Randist.

shuffle()

   my @array = $rng->shuffle(@other_array);

Given a RNG, shuffle an array.

choose()

   my @array = $rng->choose(4, @other_array);

This function fills the destination array with k objects taken randomly from the n elements of the array argument. The objects are sampled without replacement, thus each object can only appear once in destination array. It is required that k be less than or equal to n.

sample()

   my @array = $rng->sample(4, @other_array);

This method is like "choose" but 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.

GSL API Interface

Random Number Generator Types

$gsl_rng_default
$gsl_rng_knuthran
$gsl_rng_ran0
$gsl_rng_borosh13
$gsl_rng_coveyou
$gsl_rng_cmrg
$gsl_rng_fishman18
$gsl_rng_fishman20
$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 m with m = 2^31 - 1. x_n and y_n are given by the fishman20 and lecuyer21 algorithms. The seed specifies the initial value, x_1.
$gsl_rng_gfsr4
$gsl_rng_knuthran
$gsl_rng_knuthran2
$gsl_rng_knuthran2002
$gsl_rng_lecuyer21
$gsl_rng_minstd
$gsl_rng_mrg
$gsl_rng_mt19937
$gsl_rng_mt19937_1999
$gsl_rng_mt19937_1998
$gsl_rng_r250
$gsl_rng_ran0
$gsl_rng_ran1
$gsl_rng_ran2
$gsl_rng_ran3
$gsl_rng_rand - This is the BSD rand generator. Its sequence is x_{n+1} = (a x_n + c) mod m with a = 1103515245, c = 12345 and m = 2^31. The seed specifies the initial value, x_1. The period of this generator is 2^31, and it uses 1 word of storage per generator.
$gsl_rng_rand48
$gsl_rng_random128_bsd
$gsl_rng_random128_gli
$gsl_rng_random128_lib
$gsl_rng_random256_bsd
$gsl_rng_random256_gli
$gsl_rng_random256_lib
$gsl_rng_random32_bsd
$gsl_rng_random32_glib
$gsl_rng_random32_libc
$gsl_rng_random64_bsd
$gsl_rng_random64_glib
$gsl_rng_random64_libc
$gsl_rng_random8_bsd
$gsl_rng_random8_glibc
$gsl_rng_random8_libc5
$gsl_rng_random_bsd
$gsl_rng_random_glibc2
$gsl_rng_random_libc5
$gsl_rng_randu
$gsl_rng_ranf
$gsl_rng_ranlux
$gsl_rng_ranlux389
$gsl_rng_ranlxd1
$gsl_rng_ranlxd2
$gsl_rng_ranlxs0
$gsl_rng_ranlxs1
$gsl_rng_ranlxs2
$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
$gsl_rng_taus2
$gsl_rng_taus113
$gsl_rng_transputer
$gsl_rng_tt800
$gsl_rng_uni
$gsl_rng_uni32
$gsl_rng_vax - This is the VAX generator MTH$RANDOM. Its sequence is, x_{n+1} = (a x_n + c) mod m with a = 69069, c = 1 and m = 2^32. The seed specifies the initial value, x_1. The period of this generator is 2^32 and it uses 1 word of storage per generator.
$gsl_rng_waterman14
$gsl_rng_zuf - This is the ZUFALL lagged Fibonacci series generator of Peterson. Its sequence is,

 The original source code is available from NETLIB. For more information see,
 * W. Petersen, XLagged Fibonacci Random Number Generators for the NEC SX-3X, 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

The following example will print out a list a random integers between certain minimum and maximum values. The command line arguments are first the number of random numbers wanted, the minimum and then maximum. 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 encouraged. This program is available in the examples/ directory that comes with the source of this module.

If you would like a series of random non-integer numbers, then you can generate one "scaling factor" and multiple by that, such as

    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";

AUTHORS

Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2008-2021 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.

2022-05-28 perl v5.34.0