Debian Codegraph @

pod/RNG.pod

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 %perlcode %{
2
3 use Carp;
4
5 @EXPORT_OK = qw/
6 gsl_rng_alloc gsl_rng_set gsl_rng_get gsl_rng_free gsl_rng_memcpy
7 gsl_rng_fwrite gsl_rng_fread gsl_rng_clone gsl_rng_max gsl_rng_min
8 gsl_rng_name gsl_rng_size gsl_rng_state gsl_rng_print_state gsl_rng_uniform gsl_rng_uniform_pos gsl_rng_uniform_int
9 $gsl_rng_default $gsl_rng_knuthran $gsl_rng_ran0 $gsl_rng_borosh13
10 $gsl_rng_coveyou $gsl_rng_cmrg $gsl_rng_fishman18 $gsl_rng_fishman20 $gsl_rng_fishman2x
11 $gsl_rng_gfsr4 $gsl_rng_knuthran $gsl_rng_knuthran2 $gsl_rng_knuthran2002 $gsl_rng_lecuyer21
12 $gsl_rng_minstd $gsl_rng_mrg $gsl_rng_mt19937 $gsl_rng_mt19937_1999 $gsl_rng_mt19937_1998
13 $gsl_rng_r250 $gsl_rng_ran0 $gsl_rng_ran1 $gsl_rng_ran2 $gsl_rng_ran3
14 $gsl_rng_rand $gsl_rng_rand48 $gsl_rng_random128_bsd $gsl_rng_random128_gli $gsl_rng_random128_lib
15 $gsl_rng_random256_bsd $gsl_rng_random256_gli $gsl_rng_random256_lib $gsl_rng_random32_bsd
16 $gsl_rng_random32_glib $gsl_rng_random32_libc $gsl_rng_random64_bsd $gsl_rng_random64_glib
17 $gsl_rng_random64_libc $gsl_rng_random8_bsd $gsl_rng_random8_glibc $gsl_rng_random8_libc5
18 $gsl_rng_random_bsd $gsl_rng_random_glibc2 $gsl_rng_random_libc5 $gsl_rng_randu
19 $gsl_rng_ranf $gsl_rng_ranlux $gsl_rng_ranlux389 $gsl_rng_ranlxd1 $gsl_rng_ranlxd2 $gsl_rng_ranlxs0
20 $gsl_rng_ranlxs1 $gsl_rng_ranlxs2 $gsl_rng_ranmar $gsl_rng_slatec $gsl_rng_taus $gsl_rng_taus2
21 $gsl_rng_taus113 $gsl_rng_transputer $gsl_rng_tt800 $gsl_rng_uni $gsl_rng_uni32 $gsl_rng_vax
22 $gsl_rng_waterman14 $gsl_rng_zuf
23 /;
24 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
25
26 =encoding utf8
27
28 =head1 NAME
29
30 Math::GSL::RNG - Random Number Generators
31
32 =head1 SYNOPSIS
33
34 use Math::GSL::RNG;
35 my $rng = Math::GSL::RNG->new;
36 my @random = $rng->get(100);
37
38 =head1 Object Oriented Interface
39
40 =head2 Math::GSL::RNG->new($type, $seed)
41
42 my $rng = Math::GSL::RNG->new;
43 my $rng = Math::GSL::RNG->new($gsl_rng_knuthran,5);
44
45 Creates a new RNG object of type $type, seeded with $seed. Both of these
46 parameters are optional. The type $gsl_rng_default is used when no $type
47 is given.
48
49 =cut
50
51 sub new {
52 my ($class, $type, $seed) = @_;
53 $type ||= $gsl_rng_default;
54 $seed ||= int 100*rand;
55
56 my $self = {};
57 my $rng = gsl_rng_alloc($type);
58 gsl_rng_set($rng, $seed);
59
60 $self->{_rng} = $rng;
61 bless $self, $class;
62 }
63
64 =head2 copy()
65
66 my $copy = $rng->copy;
67
68 Make a copy of a RNG object.
69
70 =cut
71
72 sub copy {
73 my ($self) = @_;
74 my $copy = Math::GSL::RNG->new;
75 $copy->{_rng} = gsl_rng_clone($self->{_rng});
76
77 return $copy;
78 }
79
80 =head2 free()
81
82 $rng->free();
83
84 Free memory associated with RNG object.
85
86 =cut
87
88 sub free {
89 my ($self) = @_;
90 gsl_rng_free($self->{_rng});
91 }
92
93 =head2 name()
94
95 my $name = $rng->name();
96
97 Get the name of the RNG object as a string.
98
99 =cut
100
101 sub name {
102 my ($self) = @_;
103 gsl_rng_name($self->{_rng});
104 }
105
106 =head2 get()
107
108 my $nextval = $rng->get;
109 my (@values) = $rng->get(100);
110
111 Get the next random value from the RNG object. If given an integer N, returns the next N values.
112
113 =cut
114
115 sub get {
116 my ($self, $num_values) = @_;
117 $num_values ||= 1;
118
119 return map { gsl_rng_get($self->{_rng}) } (1 .. $num_values);
120 }
121
122 =head2 raw()
123
124 my $raw = $rng->raw();
125
126 Return the raw GSL RNG object, useful for functions which take a RNG,
127 such as the Monte Carlo integration functions or the random number
128 distribution functions in Math::GSL::Randist.
129
130 =cut
131
132 sub raw {
133 my $self = shift;
134 return $self->{_rng};
135 }
136
137 =head2 shuffle()
138
139 my @array = $rng->shuffle(@other_array);
140
141 Given a RNG, shuffle an array.
142
143 =cut
144
145 sub shuffle {
146 my ($self, @array) = @_;
147
148 my $nr_elements = $#array;
149 my $output = Math::GSL::Randist::our_gsl_ran_shuffle($self->raw,
150 [0..$nr_elements],
151 $nr_elements + 1);
152 return map { $array[$_] } @$output;
153 }
154
155 =head2 choose()
156
157 my @array = $rng->choose(4, @other_array);
158
159 This function fills the destination array with k objects taken
160 randomly from the n elements of the array argument. The objects are
161 sampled without replacement, thus each object can only appear once in
162 destination array. It is required that k be less than or equal to n.
163
164 =cut
165
166 sub choose {
167 my ($self, $k, @array) = @_;
168
169 my $nr_elements = $#array;
170 croak __PACKAGE__, "- choose method called with k greater than number of elements in the array"
171 if $k > $nr_elements + 1;
172
173 my $output = Math::GSL::Randist::our_gsl_ran_choose($self->raw, $k,
174 [0..$nr_elements],
175 $nr_elements + 1);
176 return map { $array[$_] } @$output;
177 }
178
179
180 =head2 sample()
181
182 my @array = $rng->sample(4, @other_array);
183
184 This method is like C<choose> but samples k items from the original
185 array of n items src with replacement, so the same object can appear
186 more than once in the output sequence dest. There is no requirement
187 that k be less than n in this case.
188
189 =cut
190
191 sub sample {
192 my ($self, $k, @array) = @_;
193
194 my $nr_elements = $#array;
195
196 my $output = Math::GSL::Randist::our_gsl_ran_sample($self->raw, $k,
197 [0..$nr_elements],
198 $nr_elements + 1);
199 return map { $array[$_] } @$output;
200 }
201
202
203 __END__
204
205
206 =head1 GSL API Interface
207
208 =over 1
209
210 =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.
211
212 =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.
213
214 =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).
215
216 =item gsl_rng_free($r) - This function frees all the memory associated with the generator $r.
217
218 =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.
219
220 =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).
221
222 =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.
223
224 =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.
225
226 =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.
227
228 =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.
229
230 =item gsl_rng_clone($r) - This function returns a pointer to a newly created generator which is an exact copy of the generator $r.
231
232 =item gsl_rng_max($r) - This function returns the largest value that gsl_rng_get can return.
233
234 =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.
235
236 =item gsl_rng_name($r) - This function returns a pointer to the name of the generator. For example,
237
238 =over
239
240 =item print "r is a " . gsl_rng_name($r) . "generator\n";
241
242 =item would print something like r is a 'taus' generator.
243
244 =back
245
246 =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.
247
248 =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.
249
250 =item gsl_rng_print_state($r)
251
252 =back
253
254 =head1 Random Number Generator Types
255
256 =over 1
257
258 =item $gsl_rng_default
259
260 =item $gsl_rng_knuthran
261
262 =item $gsl_rng_ran0
263
264 =item $gsl_rng_borosh13
265
266 =item $gsl_rng_coveyou
267
268 =item $gsl_rng_cmrg
269
270 =item $gsl_rng_fishman18
271
272 =item $gsl_rng_fishman20
273
274 =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.
275
276 =item $gsl_rng_gfsr4
277
278 =item $gsl_rng_knuthran
279
280 =item $gsl_rng_knuthran2
281
282 =item $gsl_rng_knuthran2002
283
284 =item $gsl_rng_lecuyer21
285
286 =item $gsl_rng_minstd
287
288 =item $gsl_rng_mrg
289
290 =item $gsl_rng_mt19937
291
292 =item $gsl_rng_mt19937_1999
293
294 =item $gsl_rng_mt19937_1998
295
296 =item $gsl_rng_r250
297
298 =item $gsl_rng_ran0
299
300 =item $gsl_rng_ran1
301
302 =item $gsl_rng_ran2
303
304 =item $gsl_rng_ran3
305
306 =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.
307
308 =item $gsl_rng_rand48
309
310 =item $gsl_rng_random128_bsd
311
312 =item $gsl_rng_random128_gli
313
314 =item $gsl_rng_random128_lib
315
316 =item $gsl_rng_random256_bsd
317
318 =item $gsl_rng_random256_gli
319
320 =item $gsl_rng_random256_lib
321
322 =item $gsl_rng_random32_bsd
323
324 =item $gsl_rng_random32_glib
325
326 =item $gsl_rng_random32_libc
327
328 =item $gsl_rng_random64_bsd
329
330 =item $gsl_rng_random64_glib
331
332 =item $gsl_rng_random64_libc
333
334 =item $gsl_rng_random8_bsd
335
336 =item $gsl_rng_random8_glibc
337
338 =item $gsl_rng_random8_libc5
339
340 =item $gsl_rng_random_bsd
341
342 =item $gsl_rng_random_glibc2
343
344 =item $gsl_rng_random_libc5
345
346 =item $gsl_rng_randu
347
348 =item $gsl_rng_ranf
349
350 =item $gsl_rng_ranlux
351
352 =item $gsl_rng_ranlux389
353
354 =item $gsl_rng_ranlxd1
355
356 =item $gsl_rng_ranlxd2
357
358 =item $gsl_rng_ranlxs0
359
360 =item $gsl_rng_ranlxs1
361
362 =item $gsl_rng_ranlxs2
363
364 =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.
365
366 =item $gsl_rng_slatec - This is the SLATEC random number generator RAND. It is ancient. The original source code is available from NETLIB.
367
368 =item $gsl_rng_taus
369
370 =item $gsl_rng_taus2
371
372 =item $gsl_rng_taus113
373
374 =item $gsl_rng_transputer
375
376 =item $gsl_rng_tt800
377
378 =item $gsl_rng_uni
379
380 =item $gsl_rng_uni32
381
382 =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.
383
384 =item $gsl_rng_waterman14
385
386 =item $gsl_rng_zuf - This is the ZUFALL lagged Fibonacci series generator of Peterson. Its sequence is,
387
388 =over
389
390 =item t = u_{n-273} + u_{n-607}
391
392 =item u_n = t - floor(t)
393
394 =back
395
396 The original source code is available from NETLIB. For more information see,
397
398 * W. Petersen, “Lagged Fibonacci Random Number Generators for the NEC SX-3”, International Journal of High Speed Computing (1994).
399
400 =back
401
402 For more informations on the functions, we refer you to the GSL official documentation:
403
404 L<http://www.gnu.org/software/gsl/manual/html_node/>
405
406
407
408 =head1 EXAMPLES
409
410 The following example will print out a list a random integers between certain
411 minimum and maximum values. The command line arguments are first the number of
412 random numbers wanted, the minimum and then maximum. The defaults are 10, 0 and
413 100, respectively.
414
415 use Math::GSL::RNG qw/:all/;
416 my $seed = int rand(100);
417 my $rng = Math::GSL::RNG->new($gsl_rng_knuthran, $seed );
418 my ($num,$min,$max) = @ARGV;
419 $num ||= 10;
420 $min ||= 0;
421 $max ||= 100;
422 print join "\n", map { $min + $rng->get % ($max-$min+1) } (1..$num);
423 print "\n";
424
425 The C<$seed> argument is optional but encouraged. This program is available in
426 the B<examples/> directory that comes with the source of this module.
427
428 If you would like a series of random non-integer numbers, then you can generate one "scaling factor"
429 and multiple by that, such as
430
431 use Math::GSL::RNG qw/:all/;
432 my $scale= rand(10);
433 my $seed = int rand(100);
434 my $rng = Math::GSL::RNG->new($gsl_rng_knuthran, $seed );
435 my ($num,$min,$max) = (10,0,100);
436 print join "\n", map { $scale*($min + $rng->get % ($max-$min+1)) } (1..$num);
437 print "\n";
438
439 =head1 AUTHORS
440
441 Jonathan "Duke" Leto <[email protected]> and Thierry Moisan <[email protected]>
442
443 =head1 COPYRIGHT AND LICENSE
444
445 Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan
446
447 This program is free software; you can redistribute it and/or modify it
448 under the same terms as Perl itself.
449
450 =cut
451
452 %}
453