Comment out some more free() calls, which need to be turned on per-subsystem to weed...
[Math-GSL.git] / pod / RNG.pod
blobc03c690da2c4337d457b305b1603b6f55b4144c4
1 %perlcode %{
2 @EXPORT_OK = qw/
3                  gsl_rng_alloc gsl_rng_set gsl_rng_get gsl_rng_free gsl_rng_memcpy
4                  gsl_rng_fwrite gsl_rng_fread gsl_rng_clone gsl_rng_max gsl_rng_min
5                  gsl_rng_name gsl_rng_size gsl_rng_state gsl_rng_print_state gsl_rng_uniform gsl_rng_uniform_pos gsl_rng_uniform_int 
6                 $gsl_rng_default $gsl_rng_knuthran $gsl_rng_ran0 $gsl_rng_borosh13
7                 $gsl_rng_coveyou $gsl_rng_cmrg $gsl_rng_fishman18 $gsl_rng_fishman20 $gsl_rng_fishman2x
8                 $gsl_rng_gfsr4 $gsl_rng_knuthran $gsl_rng_knuthran2 $gsl_rng_knuthran2002 $gsl_rng_lecuyer21
9                 $gsl_rng_minstd $gsl_rng_mrg $gsl_rng_mt19937 $gsl_rng_mt19937_1999 $gsl_rng_mt19937_1998
10                 $gsl_rng_r250 $gsl_rng_ran0 $gsl_rng_ran1 $gsl_rng_ran2 $gsl_rng_ran3
11                 $gsl_rng_rand $gsl_rng_rand48 $gsl_rng_random128_bsd $gsl_rng_random128_gli $gsl_rng_random128_lib
12                 $gsl_rng_random256_bsd $gsl_rng_random256_gli $gsl_rng_random256_lib $gsl_rng_random32_bsd
13                 $gsl_rng_random32_glib $gsl_rng_random32_libc $gsl_rng_random64_bsd $gsl_rng_random64_glib 
14                 $gsl_rng_random64_libc $gsl_rng_random8_bsd $gsl_rng_random8_glibc $gsl_rng_random8_libc5
15                 $gsl_rng_random_bsd $gsl_rng_random_glibc2 $gsl_rng_random_libc5 $gsl_rng_randu
16                 $gsl_rng_ranf $gsl_rng_ranlux $gsl_rng_ranlux389 $gsl_rng_ranlxd1 $gsl_rng_ranlxd2 $gsl_rng_ranlxs0
17                 $gsl_rng_ranlxs1 $gsl_rng_ranlxs2 $gsl_rng_ranmar $gsl_rng_slatec $gsl_rng_taus $gsl_rng_taus2
18                 $gsl_rng_taus113 $gsl_rng_transputer $gsl_rng_tt800 $gsl_rng_uni $gsl_rng_uni32 $gsl_rng_vax
19                 $gsl_rng_waterman14 $gsl_rng_zuf 
20               /;
21 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
23 =head1 NAME
25 Math::GSL::RNG - Random Number Generators
27 =head1 SYNOPSIS
29     use Math::GSL::RNG;
30     my $rng     = Math::GSL::RNG->new;
31     my @random  = $rng->get(100);
33 =head2 Math::GSL::RNG->new($type, $seed)
35     my $rng = Math::GSL::RNG->new;
36     my $rng = Math::GSL::RNG->new($gsl_rng_knuthran,5);
38 Creates a new RNG object of type $type, seeded with $seed. Both of these
39 parameters are optional. The type $gsl_rng_default is used when no $type
40 is given.
42 =cut
44 sub new {
45     my ($class, $type, $seed) = @_;
46     $type ||= $gsl_rng_default;
47     $seed ||= int 100*rand;
49     my $self = {};
50     my $rng  = gsl_rng_alloc($type);
51     gsl_rng_set($rng, $seed);
53     $self->{_rng} = $rng; 
54     bless $self, $class;
57 =head2 copy()
59     my $copy = $rng->copy;
61 Make a copy of a RNG object.
63 =cut
65 sub copy {
66     my ($self)    = @_;
67     my $copy      = Math::GSL::RNG->new;
68     $copy->{_rng} = gsl_rng_clone($self->{_rng});
70     return $copy;
73 =head2 free()
75     $rng->free();
77 Free memory associated with RNG object.
79 =cut
81 sub free {
82     my ($self)    = @_;
83     gsl_rng_free($self->{_rng});
86 =head2 name()
88    my $name = $rng->name();
90 Get the name of the RNG object as a string.
92 =cut
94 sub name {
95     my ($self)    = @_;
96     gsl_rng_name($self->{_rng});
99 =head2 get()
101     my $nextval  = $rng->get;
102     my (@values) = $rng->get(100);
104 Get the next random value from the RNG object. If given an integer N, returns the next N values.
106 =cut
108 sub get {
109     my ($self, $num_values) = @_;
110     $num_values ||= 1;
112     return map { gsl_rng_get($self->{_rng}) } (1 .. $num_values);
115 =head2 raw()
117     my $raw = $rng->raw();
119 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.
121 =cut
123 sub raw {
124     my $self = shift;
125     return $self->{_rng};
128 __END__
131 =head1 DESCRIPTION
133 =over 1
135 =item gsl_rng_alloc($T) - This function returns a pointer to a newly-created instance of a random number generator of type $T. $T must be one of the constants below. The generator is automatically initialized with the default seed, $gsl_rng_default.
137 =item gsl_rng_set($r, $s) - This function initializes (or `seeds') the random number generator. If the generator is seeded with the same value of $s on two different runs, the same stream of random numbers will be generated by successive calls to the routines below. If different values of $s are supplied, then the generated streams of random numbers should be completely different. If the seed $s is zero then the standard seed from the original implementation is used instead. For example, the original Fortran source code for the ranlux generator used a seed of 314159265, and so choosing $s equal to zero reproduces this when using $gsl_rng_ranlux.
139 =item gsl_rng_get($r) - This function 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 of min and max can determined using the auxiliary functions gsl_rng_max($r) and gsl_rng_min($r).
141 =item gsl_rng_free($r) - This function frees all the memory associated with the generator $r.
143 =item gsl_rng_memcpy($dest, $src) - This function copies the random number generator $src into the pre-existing generator $dest, making $dest into an exact copy of $src. The two generators must be of the same type.
145 =item gsl_rng_uniform($r) - This function returns a double precision floating point number uniformly distributed in the range [0,1). The range includes 0.0 but excludes 1.0. The value is typically obtained by dividing the result of gsl_rng_get($r) by gsl_rng_max($r) + 1.0 in 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). 
147 =item gsl_rng_uniform_pos($r) - This function returns a positive double precision floating point number uniformly distributed in the range (0,1), excluding both 0.0 and 1.0. The number is obtained by sampling the generator with the algorithm of gsl_rng_uniform until a non-zero value is obtained. You can use this function if you need to avoid a singularity at 0.0.
149 =item gsl_rng_uniform_int($r, $n) - This function returns a random integer from 0 to $n-1 inclusive 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 $n must be less than or equal to the range of the generator $r. If $n is larger than the range of the generator then the function calls the error handler with an error code of $GSL_EINVAL and 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 mimimum value, such as $gsl_rng_ranlxd1, $gsl_rng_mt19937 or $gsl_rng_taus, and sample it directly using gsl_rng_get. The range of each generator can be found using the auxiliary functions described in the next section. 
151 =item gsl_rng_fwrite($stream, $r) - This function writes the random number state of the random number generator $r to the stream $stream (opened with the gsl_fopen function from the Math::GSL module) in binary format. The return value is 0 for success and $GSL_EFAILED if 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.
153 =item gsl_rng_fread($stream, $r) - This function reads the random number state into the random number generator $r from the open stream $stream (opened with the gsl_fopen function from the Math::GSL module) in binary format. The random number generator $r must be preinitialized with the correct random number generator type since type information is not saved. The return value is 0 for success and $GSL_EFAILED if 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.
155 =item gsl_rng_clone($r) - This function returns a pointer to a newly created generator which is an exact copy of the generator $r.
157 =item gsl_rng_max($r) - This function returns the largest value that gsl_rng_get can return.
159 =item gsl_rng_min($r) - gsl_rng_min returns the smallest value that gsl_rng_get can return. Usually this value is zero. There are some generators with algorithms that cannot return zero, and for these generators the minimum value is 1.
161 =item gsl_rng_name($r) - This function returns a pointer to the name of the generator. For example,
163 =over
165 =item print "r is a " . gsl_rng_name($r) . "generator\n";
167 =item would print something like r is a 'taus' generator. 
169 =back
171 =item gsl_rng_size($r) - This function returns the size of the state of generator $r. You can use this information to access the state directly.
173 =item gsl_rng_state($r) - This function returns a pointer to the state of generator $r. You can use this information to access the state directly.
175 =item gsl_rng_print_state($r) 
177 =back
179 =head1 Random Number Generator Types
181 =over 1
183 =item $gsl_rng_default
185 =item $gsl_rng_knuthran
187 =item $gsl_rng_ran0
189 =item $gsl_rng_borosh13
191 =item $gsl_rng_coveyou
193 =item $gsl_rng_cmrg
195 =item $gsl_rng_fishman18
197 =item $gsl_rng_fishman20
199 =item $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. 
201 =item $gsl_rng_gfsr4
203 =item $gsl_rng_knuthran
205 =item $gsl_rng_knuthran2
207 =item $gsl_rng_knuthran2002
209 =item $gsl_rng_lecuyer21
211 =item $gsl_rng_minstd
213 =item $gsl_rng_mrg
215 =item $gsl_rng_mt19937
217 =item $gsl_rng_mt19937_1999
219 =item $gsl_rng_mt19937_1998
221 =item $gsl_rng_r250
223 =item $gsl_rng_ran0
225 =item $gsl_rng_ran1
227 =item $gsl_rng_ran2
229 =item $gsl_rng_ran3
231 =item $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. 
233 =item $gsl_rng_rand48
235 =item $gsl_rng_random128_bsd
237 =item $gsl_rng_random128_gli
239 =item $gsl_rng_random128_lib
241 =item $gsl_rng_random256_bsd
243 =item $gsl_rng_random256_gli
245 =item $gsl_rng_random256_lib
247 =item $gsl_rng_random32_bsd
249 =item $gsl_rng_random32_glib
251 =item $gsl_rng_random32_libc
253 =item $gsl_rng_random64_bsd
255 =item $gsl_rng_random64_glib
257 =item $gsl_rng_random64_libc
259 =item $gsl_rng_random8_bsd
261 =item $gsl_rng_random8_glibc
263 =item $gsl_rng_random8_libc5
265 =item $gsl_rng_random_bsd
267 =item $gsl_rng_random_glibc2
269 =item $gsl_rng_random_libc5
271 =item $gsl_rng_randu
273 =item $gsl_rng_ranf
275 =item $gsl_rng_ranlux
277 =item $gsl_rng_ranlux389
279 =item $gsl_rng_ranlxd1
281 =item $gsl_rng_ranlxd2
283 =item $gsl_rng_ranlxs0
285 =item $gsl_rng_ranlxs1
287 =item $gsl_rng_ranlxs2
289 =item $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.
291 =item $gsl_rng_slatec - This is the SLATEC random number generator RAND. It is ancient. The original source code is available from NETLIB.
293 =item $gsl_rng_taus
295 =item $gsl_rng_taus2
297 =item $gsl_rng_taus113
299 =item $gsl_rng_transputer
301 =item $gsl_rng_tt800
303 =item $gsl_rng_uni
305 =item $gsl_rng_uni32
307 =item $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. 
309 =item $gsl_rng_waterman14
311 =item $gsl_rng_zuf - This is the ZUFALL lagged Fibonacci series generator of Peterson. Its sequence is,
313 =over
315 =item          t = u_{n-273} + u_{n-607}
317 =item          u_n  = t - floor(t)
318      
319 =back
321  The original source code is available from NETLIB. For more information see,
323  * W. Petersen, “Lagged Fibonacci Random Number Generators for the NEC SX-3”, International Journal of High Speed Computing (1994). 
325 =back
327 For more informations on the functions, we refer you to the GSL offcial documentation: 
329 L<http://www.gnu.org/software/gsl/manual/html_node/>
331 Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/ name_of_the_function_you_want
333 =head1 EXAMPLES
335 The following example will print out a list a random integers between certain
336 minimum and maximum values. The command line arguments are first the number of
337 random numbers wanted, the minimum and then maximum. The defaults are 10, 0 and
338 100, respectively. 
340     use Math::GSL::RNG qw/:all/;
341     my $seed = int rand(100);
342     my $rng  = Math::GSL::RNG->new($gsl_rng_knuthran, $seed );
343     my ($num,$min,$max) = @ARGV;
344     $num ||= 10;
345     $min ||= 0;
346     $max ||= 100;
347     print join "\n", map { $min + $rng->get % ($max-$min+1)  } (1..$num);
348     print "\n";
350 The C<$seed> argument is optional but encouraged. This program is available in
351 the B<examples/> directory that comes with the source of this module.
353 If you would like a series of random non-integer numbers, then you can generate one "scaling factor" 
354 and multiple by that, such as
356     use Math::GSL::RNG qw/:all/;
357     my $scale= rand(10);
358     my $seed = int rand(100);
359     my $rng  = Math::GSL::RNG->new($gsl_rng_knuthran, $seed );
360     my ($num,$min,$max) = (10,0,100);
361     print join "\n", map { $scale*($min + $rng->get % ($max-$min+1))  } (1..$num);
362     print "\n";
364 =head1 AUTHORS
366 Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
368 =head1 COPYRIGHT AND LICENSE
370 Copyright (C) 2008-2009 Jonathan Leto and Thierry Moisan
372 This program is free software; you can redistribute it and/or modify it
373 under the same terms as Perl itself.
375 =cut