Added cwres module
[PsN.git] / modules / Math-Random / Random.pm
blob59d3fa7cd20ed85552c59f1b6024e7472e830df0
1 package Math::Random;
3 use strict;
4 use Carp;
5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
7 require Exporter;
8 require DynaLoader;
9 require AutoLoader;
11 @ISA = qw(Exporter DynaLoader);
12 $VERSION = '0.67';
14 @EXPORT = qw(random_normal
15 random_permutation
16 random_permuted_index
17 random_uniform
18 random_uniform_integer
19 random_seed_from_phrase
20 random_get_seed
21 random_set_seed_from_phrase
22 random_set_seed
25 @EXPORT_OK = qw(random_beta
26 random_chi_square
27 random_exponential
28 random_f
29 random_gamma
30 random_multivariate_normal
31 random_multinomial
32 random_noncentral_chi_square
33 random_noncentral_f
34 random_normal
35 random_permutation
36 random_permuted_index
37 random_uniform
38 random_poisson
39 random_uniform_integer
40 random_negative_binomial
41 random_binomial
42 random_seed_from_phrase
43 random_get_seed
44 random_set_seed_from_phrase
45 random_set_seed
48 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
50 sub AUTOLOAD {
51 # This AUTOLOAD is used to 'autoload' constants from the constant()
52 # XS function. If a constant is not found then control is passed
53 # to the AUTOLOAD in AutoLoader.
55 my $constname;
56 ($constname = $AUTOLOAD) =~ s/.*:://;
57 croak "& not defined" if $constname eq 'constant';
58 my $val = constant($constname, @_ ? $_[0] : 0);
59 if ($! != 0) {
60 if ($! =~ /Invalid/) {
61 $AutoLoader::AUTOLOAD = $AUTOLOAD;
62 goto &AutoLoader::AUTOLOAD;
64 else {
65 croak "Your vendor has not defined Math::Random macro $constname";
68 *$AUTOLOAD = sub () { $val };
69 goto &$AUTOLOAD;
72 bootstrap Math::Random $VERSION;
75 ### set seeds by default
76 salfph(scalar(localtime()));
78 #####################################################################
79 # RANDOM DEVIATE GENERATORS #
80 #####################################################################
82 sub random_beta { # Arguments: ($n,$aa,$bb)
83 croak "Usage: random_beta(\$n,\$aa,\$bb)" if scalar(@_) < 3;
84 my($n, $aa, $bb) = @_;
85 croak("($aa = \$aa < 1.0E-37) or ($bb = \$bb < 1.0E-37)\nin ".
86 "random_beta(\$n,\$aa,\$bb)")
87 if (($aa < 1.0E-37) or ($bb < 1.0E-37));
88 return genbet($aa,$bb) unless wantarray();
89 my $val;
90 my @ans = (0) x $n;
91 foreach $val (@ans) { $val = genbet($aa,$bb); }
92 return @ans;
95 sub random_chi_square { # Arguments: ($n,$df)
96 croak "Usage: random_chi_square(\$n,\$df)" if scalar(@_) < 2;
97 my($n, $df) = @_;
98 croak "$df = \$df <= 0\nin random_chi_square(\$n,\$df)" if ($df <= 0);
99 return genchi($df) unless wantarray();
100 my $val;
101 my @ans = (0) x $n;
102 foreach $val (@ans) { $val = genchi($df); }
103 return @ans;
106 sub random_exponential { # Arguments: ($n,$av), defaults (1,1)
107 return wantarray() ? (genexp(1)) : genexp(1)
108 if scalar(@_) == 0; # default behavior if no arguments
109 my($n, $av) = @_;
110 $av = 1 unless defined($av); # default $av is 1
111 croak "$av = \$av < 0\nin random_exponential(\$n,\$av)" if ($av < 0);
112 return genexp($av) unless wantarray();
113 my $val;
114 my @ans = (0) x $n;
115 foreach $val (@ans) { $val = genexp($av); }
116 return @ans;
119 sub random_f { # Arguments: ($n,$dfn,$dfd)
120 croak "Usage: random_f(\$n,\$dfn,\$dfd)" if scalar(@_) < 3;
121 my($n, $dfn, $dfd) = @_;
122 croak("($dfn = \$dfn <= 0) or ($dfd = \$dfd <= 0)\nin ".
123 "random_f(\$n,\$dfn,\$dfd)") if (($dfn <= 0) or ($dfd <= 0));
124 return genf($dfn,$dfd) unless wantarray();
125 my $val;
126 my @ans = (0) x $n;
127 foreach $val (@ans) { $val = genf($dfn,$dfd); }
128 return @ans;
131 sub random_gamma { # Arguments: ($n,$a,$r)
132 croak "Usage: random_gamma(\$n,\$a,\$r)" if scalar(@_) < 3;
133 my($n, $a, $r) = @_;
134 croak "($a = \$a <= 0) or ($r = \$r <= 0)\nin random_gamma(\$n,\$a,\$r)"
135 if (($a <= 0) or ($r <= 0));
136 return gengam($a,$r) unless wantarray();
137 my $val;
138 my @ans = (0) x $n;
139 foreach $val (@ans) { $val = gengam($a,$r); }
140 return @ans;
143 sub random_multivariate_normal { # Arguments: ($n, @mean, @covar(2-dim'l))
145 croak "Usage: random_multivariate_normal(\$n,\@mean,\@covar(2-dim'l))"
146 if (scalar(@_)) < 3;
147 my $n = shift(@_); # first element is number of obs. desired
148 my $p = scalar(@_)/2; # best guess at dimension of deviate
150 # check outline of arguments
151 croak("Sizes of \@mean and \@covar don't match\nin ".
152 "random_multivariate_normal(\$n, \@mean, \@covar(2-dim'l))")
153 unless (($p == int($p)) and ("$_[$p - 1]" !~ /^ARRAY/) and
154 ("$_[$p]" =~ /^ARRAY/));
156 # linearize input - it seems faster to push
157 my @linear = ();
159 push @linear, splice(@_, 0, $p); # fill first $p slots w/ mean
161 # expand array references
162 my $ref;
163 foreach $ref (@_) { # for the rest of the input
165 # check length of row of @covariance
166 croak("\@covar is not a $p by $p array ($p is size of \@mean)\nin ".
167 "random_multivariate_normal(\$n, \@mean, \@covar(2-dim'l))")
168 unless (scalar(@{$ref}) == $p);
170 push @linear, @{$ref};
173 # load float working array with linearized input
174 putflt(@linear) or
175 croak "Unable to allocate memory\nin random_multivariate_normal";
177 # initialize parameter array for multivariate normal generator
178 psetmn($p) or
179 croak "Unable to allocate memory\nin random_multivariate_normal";
181 unless (wantarray()) {
182 ### if called in a scalar context, returns single refernce to obs
183 pgenmn();
184 return [ getflt($p) ];
187 # otherwise return an $n by $p array of obs.
188 my @ans = (0) x $n;
189 foreach $ref (@ans) {
190 pgenmn();
191 $ref = [ getflt($p) ];
193 return @ans;
196 sub random_multinomial { # Arguments: ($n,@p)
197 my($n, @p) = @_;
198 my $ncat = scalar(@p); # number of categories
199 $n = int($n);
200 croak "$n = \$n < 0\nin random_multinomial(\$n,\@p)" if ($n < 0);
201 croak "$ncat = (length of \@p) < 2\nin random_multinomial(\$n,\@p)"
202 if ($ncat < 2);
203 rspriw($ncat) or croak "Unable to allocate memory\nin random_multinomial";
204 my($i,$sum,$val) = (0,0,0);
205 pop @p;
206 rsprfw(scalar(@p)) or
207 croak "Unable to allocate memory\nin random_multinomial";
208 foreach $val (@p) {
209 croak "$val = (some \$p[i]) < 0 or > 1\nin random_multinomial(\$n,\@p)"
210 if (($val < 0) or ($val > 1));
211 svprfw($i,$val);
212 $i++;
213 $sum += $val;
215 croak "Sum of \@p > 1\nin random_multinomial(\$n,\@p)" if ($sum > 0.99999);
216 pgnmul($n, $ncat);
217 ### get the results
218 $i = 0;
219 foreach $val (@p) {
220 $val = gvpriw($i);
221 $i++;
223 push @p, gvpriw($i);
224 return @p;
227 sub random_noncentral_chi_square { # Arguments: ($n,$df,$nonc)
228 croak "Usage: random_noncentral_chi_square(\$n,\$df,\$nonc)"
229 if scalar(@_) < 3;
230 my($n, $df, $nonc) = @_;
231 croak("($df = \$df < 1) or ($nonc = \$nonc) < 0\n".
232 "in random_noncentral_chi_square(\$n,\$df,\$nonc)")
233 if (($df < 1) or ($nonc < 0));
234 return gennch($df,$nonc) unless wantarray();
235 my $val;
236 my @ans = (0) x $n;
237 foreach $val (@ans) { $val = gennch($df,$nonc); }
238 return @ans;
241 sub random_noncentral_f { # Arguments: ($n,$dfn,$dfd,$nonc)
242 croak "Usage: random_noncentral_f(\$n,\$dfn,\$dfd,\$nonc)"
243 if scalar(@_) < 4;
244 my($n, $dfn, $dfd, $nonc) = @_;
245 croak("($dfn = \$dfn < 1) or ($dfd = \$dfd <= 0) or ($nonc ".
246 "= \$nonc < 0)\nin random_noncentral_f(\$n,\$dfn,\$dfd,\$nonc)")
247 if (($dfn < 1) or ($dfd <= 0) or ($nonc < 0));
248 return gennf($dfn,$dfd,$nonc) unless wantarray();
249 my $val;
250 my @ans = (0) x $n;
251 foreach $val (@ans) { $val = gennf($dfn,$dfd,$nonc); }
252 return @ans;
255 sub random_normal { # Arguments: ($n,$av,$sd), defaults (1,0,1)
256 return wantarray() ? (gennor(0,1)) : gennor(0,1)
257 if scalar(@_) == 0; # default behavior if no arguments
258 my($n, $av, $sd) = @_;
259 $av = 0 unless defined($av); # $av defaults to 0
260 $sd = 1 unless defined($sd); # $sd defaults to 1, even if $av specified
261 croak "$sd = \$sd < 0\nin random_normal([\$n[,\$av[,\$sd]]])" if ($sd < 0);
262 return gennor($av,$sd) unless wantarray();
263 my $val;
264 my @ans = (0) x $n;
265 foreach $val (@ans) { $val = gennor($av,$sd); }
266 return @ans;
269 sub random_permutation { # Argument: (@array) - array to be permuted.
270 my $n = scalar(@_); # number of elements to be permuted
271 return () if $n == 0;
272 rspriw($n) or
273 croak "Unable to allocate memory\nin random_permutation";
274 pgnprm($n);
275 my($val, $i) = (0,0);
276 my @ans = (0) x $n;
277 foreach $val (@ans) {
278 $val = gvpriw($i);
279 $i++;
281 return @_[@ans];
284 sub random_permuted_index { # Argument: $n = scalar(@array) (for permutation)
285 croak "Usage: random_permuted_index(\$n)" if scalar(@_) < 1;
286 my $n = int(shift(@_)); # number of elements to be permuted
287 croak "$n = \$n < 0 in random_permuted_index(\$n)" if $n < 0;
288 return () if $n == 0;
289 rspriw($n) or
290 croak "Unable to allocate memory\nin random_permuted_index";
291 pgnprm($n);
292 my($val, $i) = (0,0);
293 my @ans = (0) x $n;
294 foreach $val (@ans) {
295 $val = gvpriw($i);
296 $i++;
298 return @ans;
301 sub random_uniform { # Arguments: ($n,$low,$high), defaults (1,0,1)
302 return wantarray() ? (genunf(0,1)) : genunf(0,1)
303 if scalar(@_) == 0;
304 croak "Usage: random_uniform([\$n,[\$low,\$high]])"
305 if scalar(@_) == 2; # only default is (0,1) for ($low,$high) both undef
306 my($n, $low, $high) = @_;
307 $low = 0 unless defined($low); # default for $low is 0
308 $high = 1 unless defined($high); # default for $high is 1
309 croak("$low = \$low > \$high = $high\nin ".
310 "random_uniform([\$n,[\$low,\$high]])") if ($low > $high);
311 return genunf($low,$high) unless wantarray();
312 my $val;
313 my @ans = (0) x $n;
314 foreach $val (@ans) { $val = genunf($low,$high); }
315 return @ans;
318 sub random_poisson { # Arguments: ($n, $mu)
319 croak "Usage: random_poisson(\$n,\$mu)" if scalar(@_) < 2;
320 my($n, $mu) = @_;
321 croak "$mu = \$mu < 0\nin random_poisson(\$n,\$mu)" if ($mu < 0);
322 return ignpoi($mu) unless wantarray();
323 my $val;
324 my @ans = (0) x $n;
325 foreach $val (@ans) { $val = ignpoi($mu); }
326 return @ans;
329 sub random_uniform_integer { # Arguments: ($n,$low,$high)
330 croak "Usage: random_uniform_integer(\$n,\$low,\$high)" if scalar(@_) < 3;
331 my($n, $low, $high) = @_;
332 $low = int($low);
333 $high = int($high);
334 croak("$low = \$low > \$high = $high\nin ".
335 "random_uniform_integer(\$n,\$low,\$high)") if ($low > $high);
336 my $range = $high - $low;
337 croak("$range = (\$high - \$low) > 2147483561\nin ".
338 "random_uniform_integer(\$n,\$low,\$high)") if ($range > 2147483561);
339 return ($low + ignuin(0,$range)) unless wantarray();
340 my $val;
341 my @ans = (0) x $n;
342 foreach $val (@ans) { $val = $low + ignuin(0,$range); }
343 return @ans;
346 sub random_negative_binomial { # Arguments: ($n,$ne,$p)
347 croak "Usage: random_negative_binomial(\$n,\$ne,\$p)" if scalar(@_) < 3;
348 my($n, $ne, $p) = @_;
349 $ne = int($ne);
350 croak("($ne = \$ne <= 0) or ($p = \$p <= 0 or >= 1)\nin ".
351 "random_negative_binomial(\$n,\$ne,\$p)")
352 if (($ne <= 0) or (($p <= 0) or ($p >= 1)));
353 return ignnbn($ne,$p) unless wantarray();
354 my $val;
355 my @ans = (0) x $n;
356 foreach $val (@ans) { $val = ignnbn($ne,$p); }
357 return @ans;
360 sub random_binomial { # Arguments: ($n,$nt,$p)
361 croak "Usage: random_binomial(\$n,\$nt,\$p)" if scalar(@_) < 3;
362 my($n, $nt, $p) = @_;
363 $nt = int($nt);
364 croak("($nt = \$nt < 0) or ($p = \$p < 0 or > 1)\nin ".
365 "random_binomial(\$n,\$nt,\$p)")
366 if (($nt < 0) or (($p < 0) or ($p > 1)));
367 return ignbin($nt,$p) unless wantarray();
368 my $val;
369 my @ans = (0) x $n;
370 foreach $val (@ans) { $val = ignbin($nt,$p); }
371 return @ans;
374 #####################################################################
375 # SEED HANDLER FUNCTIONS #
376 #####################################################################
378 sub random_seed_from_phrase { # Argument $phrase
379 my $phrase = shift(@_);
380 $phrase ||= "";
381 return phrtsd($phrase);
384 sub random_get_seed { # no argument
385 return getsd();
388 sub random_set_seed_from_phrase { # Argument $phrase
389 my $phrase = shift(@_);
390 $phrase ||= "";
391 salfph($phrase);
392 return 1;
395 sub random_set_seed { # Argument @seed
396 my($seed1,$seed2) = @_;
397 croak("Usage: random_set_seed(\@seed)\n\@seed[0,1] must be two integers ".
398 "in the range (1,1) to (2147483562,2147483398)\nand usually comes ".
399 "from a call to random_get_seed() ".
400 "or\nrandom_seed_from_phrase(\$phrase).")
401 unless (((($seed1 == int($seed1)) and ($seed2 == int($seed2))) and
402 (($seed1 > 0) and ($seed2 > 0))) and
403 (($seed1 < 2147483563) and ($seed2 < 2147483399)));
404 setall($seed1,$seed2);
405 return 1;
408 #####################################################################
409 # HELPER ROUTINES #
410 # These use the C work arrays and are not intended for export #
411 # (Currently only used in random_multivariate_normal) #
412 #####################################################################
414 sub getflt {
415 my $n = $_[0];
416 my $val;
417 my $i = 0;
418 my @junk = (0) x $n;
419 foreach $val (@junk) {
420 $val = gvprfw($i);
421 $i++;
423 return @junk;
426 sub putflt {
427 my $n = scalar(@_);
428 rsprfw($n) or return 0;
429 my $val;
430 my $i = 0;
431 foreach $val (@_) { # load up floats
432 svprfw($i,$val);
433 $i++;
435 return 1;
438 # Autoload methods go after =cut, and are processed by the autosplit program.
442 __END__
444 =head1 NAME
446 B<Math::Random> - Random Number Generators
448 =head1 SYNOPSIS
450 =over 4
452 =item *
454 use Math::Random;
456 Exports the following routines by default (see L<DEFAULT ROUTINES>):
458 random_set_seed_from_phrase
459 random_get_seed
460 random_seed_from_phrase
461 random_set_seed
462 random_uniform
463 random_uniform_integer
464 random_permutation
465 random_permuted_index
466 random_normal
468 In this case the extended routines (see L<"EXTENDED ROUTINES">) can be
469 used by qualifying them explicitly with C<Math::Random::>, for
470 example: C<$stdexp = Math::Random::random_exponential();>
472 =item *
474 use Math::Random qw(random_beta
475 random_chi_square
476 random_exponential
477 random_f
478 random_gamma
479 random_multivariate_normal
480 random_multinomial
481 random_noncentral_chi_square
482 random_noncentral_f
483 random_normal
484 random_permutation
485 random_permuted_index
486 random_uniform
487 random_poisson
488 random_uniform_integer
489 random_negative_binomial
490 random_binomial
491 random_seed_from_phrase
492 random_get_seed
493 random_set_seed_from_phrase
494 random_set_seed );
496 Exports all the routines explicitly. Use a subset of the list for the
497 routines you want.
499 =item *
501 use Math::Random qw(:all);
503 Exports all the routines, as well.
505 =back
507 =head1 DESCRIPTION
509 B<Math::Random> is a B<Perl> port of the B<C> version of B<randlib>,
510 which is a suite of routines for generating random deviates. See
511 L<"RANDLIB"> for more information.
513 This port supports all of the distributions from which the B<Fortran>
514 and B<C> versions generate deviates. The major functionalities that
515 are excluded are the multiple generators/splitting facility and
516 antithetic random number generation. These facilities, along with
517 some of the distributions which I<are> included, are probably not of
518 interest except to the very sophisticated user. If there is
519 sufficient interest, the excluded facilities will be included in a
520 future release. The code to perform the excluded facilities is
521 available as B<randlib> in B<Fortran> and B<C> source.
523 =head2 Default routines:
525 The routines which are exported by default are the only ones that the
526 average B<Perl> programmer is likely to need.
528 =over 4
530 =item C<random_set_seed_from_phrase($phrase)>
532 Sets the seed of the base generator to a value determined by
533 I<$phrase>. The value used for a given I<$phrase> is consistent from
534 implementation to implementation (it does not rely on the machine
535 collating sequence). B<Note:> When the B<Perl> processor loads
536 package B<Math::Random> the seed is set to a value based on the
537 current time. The seed changes each time B<Math::Random> generates
538 something random.
540 The ability to set the seed is useful for debugging, or for those who
541 like reproducible runs.
543 =item C<random_get_seed()>
545 Returns an array of length two which contains the two integers
546 constituting the seed (assuming a call in array context). An
547 invocation in a scalar context returns the integer 2, which is
548 probably not useful.
550 =item C<random_seed_from_phrase($phrase)>
552 Returns an array of length two which contains the two integers
553 consituting the seed (assuming a call in array context). An
554 invocation in a scalar context returns the integer 2, which is
555 probably not useful. The seed generated is the seed used to set the
556 seed in a call to C<random_set_seed_from_phrase>.
558 B<Note:> the following two calls (for the same I<$phrase>) are
559 equivalent:
561 random_set_seed(random_seed_from_phrase($phrase));
565 random_set_seed_from_phrase($phrase);
567 =item C<random_set_seed(@seed)>
569 Sets the seed of the base generator to the value I<@seed>[0,1].
570 Usually, the argument I<@seed> should be the result of a call to
571 C<random_get_seed> or C<random_seed_from_phrase>. I<@seed>[0,1] must
572 be two integers in the range S<(1, 1)> to S<(2147483562, 2147483398)>,
573 inclusive.
575 =item C<random_uniform($n, $low, $high)>
577 =item C<random_uniform($n)>
579 =item C<random_uniform()>
581 When called in an array context, returns an array of I<$n> deviates
582 generated from a I<uniform($low,>S< >I<$high)> distribution. When
583 called in a scalar context, generates and returns only one such
584 deviate as a scalar, regardless of the value of I<$n>.
586 Argument restrictions: I<$low> must be less than or equal to I<$high>.
588 Defaults are (1, 0, 1). B<Note:> I<$high> must be specified if
589 I<$low> is specified.
591 =item C<random_uniform_integer($n, $low, $high)>
593 When called in an array context, returns an array of I<$n> integer
594 deviates generated from a I<uniform($low,>S< >I<$high)> distribution
595 on the integers. When called in a scalar context, generates and
596 returns only one such deviate as a scalar, regardless of the value of
597 I<$n>.
599 Argument restrictions: I<$low> and I<$high> are first rounded using
600 C<int()>; the resulting I<$low> must be less than or equal to I<$high>,
601 and the resulting range I<($high - $low)> must not be greater than
602 2147483561.
604 There are no defaults; all three arguments must be provided.
606 =item C<random_permutation(@array)>
608 Returns I<@array>, randomly permuted.
610 =item C<random_permuted_index($n)>
612 Returns an array of array indices, randomly permuted. The indices
613 used are S<(0, ... ,>(I<$n>S< - >1)). This produces the indices used
614 by C<random_permutation> for a given seed, without passing arrays.
616 B<Note:> the following are equivalent:
618 random_set_seed_from_phrase('jjv');
619 random_permutation(@array);
623 random_set_seed_from_phrase('jjv');
624 @array[(random_permuted_index(scalar(@array)))];
626 =item C<random_normal($n, $av, $sd)>
628 =item C<random_normal($n, $av)>
630 =item C<random_normal($n)>
632 =item C<random_normal()>
634 When called in an array context, returns an array of I<$n> deviates
635 generated from a I<normal($av, $sd^2)> distribution. When called in a
636 scalar context, generates and returns only one such deviate as a
637 scalar, regardless of the value of I<$n>.
639 Argument restrictions: I<$sd> must be non-negative.
641 Defaults are (1, 0, 1).
643 =back
645 =head2 Extended Routines:
647 These routines generate deviates from many other distributions.
649 B<Note:> The parameterizations of these deviates are standard (insofar
650 as there I<is> a standard ... ) but particular attention should be
651 paid to the distributions of the I<beta> and I<gamma> deviates (noted
652 in C<random_beta> and C<random_gamma> below).
654 =over 4
656 =item C<random_beta($n, $aa, $bb)>
658 When called in an array context, returns an array of I<$n> deviates
659 generated from the I<beta> distribution with parameters I<$aa> and
660 I<$bb>. The density of the beta is:
662 X^(I<$aa> - 1) * (1 - X)^(I<$bb> - 1) / S<B>(I<$aa> , I<$bb>) for 0 < X <
665 When called in a scalar context, generates and returns only one such
666 deviate as a scalar, regardless of the value of I<$n>.
668 Argument restrictions: Both I<$aa> and I<$bb> must not be less than
669 C<1.0E-37>.
671 There are no defaults; all three arguments must be provided.
673 =item C<random_binomial($n, $nt, $p)>
675 When called in an array context, returns an array of I<$n> outcomes
676 generated from the I<binomial> distribution with number of trials
677 I<$nt> and probability of an event in each trial I<$p>. When called
678 in a scalar context, generates and returns only one such outcome as a
679 scalar, regardless of the value of I<$n>.
681 Argument restrictions: I<$nt> is rounded using C<int()>; the result
682 must be non-negative. I<$p> must be between 0 and 1 inclusive.
684 There are no defaults; both arguments must be provided.
686 =item C<random_chi_square($n, $df)>
688 When called in an array context, returns an array of I<$n> deviates
689 generated from the I<chi-square> distribution with I<$df> degrees of
690 freedom. When called in a scalar context, generates and returns only
691 one such deviate as a scalar, regardless of the value of I<$n>.
693 Argument restrictions: I<$df> must be positive.
695 There are no defaults; both arguments must be provided.
697 =item C<random_exponential($n, $av)>
699 =item C<random_exponential($n)>
701 =item C<random_exponential()>
703 When called in an array context, returns an array of I<$n> deviates
704 generated from the I<exponential> distribution with mean I<$av>. When
705 called in a scalar context, generates and returns only one such
706 deviate as a scalar, regardless of the value of I<$n>.
708 Argument restrictions: I<$av> must be non-negative.
710 Defaults are (1, 1).
712 =item C<random_f($n, $dfn, $dfd)>
714 When called in an array context, returns an array of I<$n> deviates
715 generated from the I<F> (variance ratio) distribution with degrees of
716 freedom I<$dfn> (numerator) and I<$dfd> (denominator). When called in
717 a scalar context, generates and returns only one such deviate as a
718 scalar, regardless of the value of I<$n>.
720 Argument restrictions: Both I<$dfn> and I<$dfd> must be positive.
722 There are no defaults; all three arguments must be provided.
724 =item C<random_gamma($n, $a, $r)>
726 When called in an array context, returns an array of I<$n> deviates
727 generated from the I<gamma> distribution with parameters I<$a> and
728 I<$r>. The density of the gamma is:
730 (I<$a>**I<$r>) / Gamma(I<$r>) * X**(I<$r> - 1) * Exp(-I<$a>*X)
732 When called in a scalar context, generates and returns only one such
733 deviate as a scalar, regardless of the value of I<$n>.
735 Argument restrictions: Both I<$a> and I<$r> must be positive.
737 There are no defaults; all three arguments must be provided.
739 =item C<random_multinomial($n, @p)>
741 When called in an array context, returns single observation from the
742 I<multinomial> distribution, with I<$n> events classified into as many
743 categories as the length of I<@p>. The probability of an event being
744 classified into category I<i> is given by the I<i>th element of I<@p>.
745 The observation is an array with length equal to I<@p>, so when called
746 in a scalar context it returns the length of @p. The sum of the
747 elements of the observation is equal to I<$n>.
749 Argument restrictions: I<$n> is rounded with C<int()> before it is
750 used; the result must be non-negative. I<@p> must have length at
751 least 2. All elements of I<@p> except the last must be between 0 and
752 1 inclusive, and sum to no more than 0.99999. B<Note:> The last
753 element of I<@p> is a dummy to indicate the number of categories, and
754 it is adjusted to bring the sum of the elements of I<@p> to 1.
756 There are no defaults; both arguments must be provided.
758 =item C<random_multivariate_normal($n, @mean, @covar)>
760 When called in an array context, returns an array of I<$n> deviates
761 (each deviate being an array reference) generated from the
762 I<multivariate normal> distribution with mean vector I<@mean> and
763 variance-covariance matrix I<@covar>. When called in a scalar
764 context, generates and returns only one such deviate as an array
765 reference, regardless of the value of I<$n>.
767 Argument restrictions: If the dimension of the deviate to be generated
768 is I<p>, I<@mean> should be a length I<p> array of real numbers.
769 I<@covar> should be a length I<p> array of references to length I<p>
770 arrays of real numbers (i.e. a I<p> by I<p> matrix). Further,
771 I<@covar> should be a symmetric positive-definite matrix, although the
772 B<Perl> code does not check positive-definiteness, and the underlying
773 B<C> code assumes the matrix is symmetric. Given that the
774 variance-covariance matrix is symmetric, it doesn't matter if the
775 references refer to rows or columns. If a non-positive definite
776 matrix is passed to the function, it will abort with the following
777 message:
779 COVM not positive definite in SETGMN
781 Also, a non-symmetric I<@covar> may produce deviates without
782 complaint, although they may not be from the expected distribution.
783 For these reasons, you are encouraged to I<verify the arguments
784 passed>.
786 The B<Perl> code I<does> check the dimensionality of I<@mean> and
787 I<@covar> for consistency. It does so by checking that the length of
788 the argument vector passed is odd, that what should be the last
789 element of I<@mean> and the first element of I<@covar> look like they
790 are a number followed by an array reference respectively, and that the
791 arrays referred to in I<@covar> are as long as I<@mean>.
793 There are no defaults; all three arguments must be provided.
795 =item C<random_negative_binomial($n, $ne, $p)>
797 When called in an array context, returns an array of I<$n> outcomes
798 generated from the I<negative binomial> distribution with number of
799 events I<$ne> and probability of an event in each trial I<$p>. When
800 called in a scalar context, generates and returns only one such
801 outcome as a scalar, regardless of the value of I<$n>.
803 Argument restrictions: I<$ne> is rounded using C<int()>, the result
804 must be positive. I<$p> must be between 0 and 1 exclusive.
806 There are no defaults; both arguments must be provided.
808 =item C<random_noncentral_chi_square($n, $df, $nonc)>
810 When called in an array context, returns an array of I<$n> deviates
811 generated from the I<noncentral chi-square> distribution with I<$df>
812 degrees of freedom and noncentrality parameter I<$nonc>. When called
813 in a scalar context, generates and returns only one such deviate as a
814 scalar, regardless of the value of I<$n>.
816 Argument restrictions: I<$df> must be at least 1, I<$nonc> must be
817 non-negative.
819 There are no defaults; all three arguments must be provided.
821 =item C<random_noncentral_f($n, $dfn, $dfd, $nonc)>
823 When called in an array context, returns an array of I<$n> deviates
824 generated from the I<noncentral F> (variance ratio) distribution with
825 degrees of freedom I<$dfn> (numerator) and I<$dfd> (denominator); and
826 noncentrality parameter I<$nonc>. When called in a scalar context,
827 generates and returns only one such deviate as a scalar, regardless of
828 the value of I<$n>.
830 Argument restrictions: I<$dfn> must be at least 1, I<$dfd> must be
831 positive, and I<$nonc> must be non-negative.
833 There are no defaults; all four arguments must be provided.
835 =item C<random_poisson($n, $mu)>
837 When called in an array context, returns an array of I<$n> outcomes
838 generated from the I<Poisson> distribution with mean I<$mu>. When
839 called in a scalar context, generates and returns only one such
840 outcome as a scalar, regardless of the value of I<$n>.
842 Argument restrictions: I<$mu> must be non-negative.
844 There are no defaults; both arguments must be provided.
846 =back
848 =head1 ERROR HANDLING
850 The B<Perl> code should C<croak> if bad arguments are passed or if the
851 underlying B<C> code cannot allocate the necessary memory. The only
852 error which should kill the job without C<croak>ing is a non-positive
853 definite variance-covariance matrix passed to
854 C<random_multivarite_normal> (see L<"EXTENDED ROUTINES">).
856 =head1 RANDLIB
858 B<randlib> is available in B<Fortran> and B<C> source form, and will
859 soon be available in B<Fortran90> source as well. B<randlib.c> can be
860 obtained from B<statlib>. Send mail whose message is I<'send
861 randlib.c.shar from general'> to:
863 statlib@lib.stat.cmu.edu
865 B<randlib.c> can also be obtained by anonymous B<ftp> to:
867 odin.mdacc.tmc.edu (143.111.62.32)
869 where it is available as
871 /pub/source/randlib.c-1.3.tar.gz
873 For obvious reasons, the original B<randlib> (in B<Fortran>) has been
874 renamed to
876 /pub/source/randlib.f-1.3.tar.gz
878 on the same machine.
880 Our FTP index is on file C<./pub/index>.
882 If you have Internet access and a browser you might note the following
883 web site addresses:
885 University of Texas M. D. Anderson Cancer Center Home Page:
887 http://utmdacc.mdacc.tmc.edu/
889 Department of Biomathematics Home Page:
891 http://odin.mdacc.tmc.edu/
893 Available Software:
895 http://odin.mdacc.tmc.edu/anonftp/
897 =head1 SUPPORT
899 This work was supported in part by grant CA-16672 from the National
900 Cancer Institute. We are grateful to Larry and Pat McNeil of Corpus
901 Cristi for their generous support. Some equipment used in this effort
902 was provided by IBM as part of a cooperative study agreement; we thank
903 them.
905 =head1 CODE MANIPULATION
907 The B<C> version of B<randlib> was obtained by translating the
908 original B<Fortran> B<randlib> using B<PROMULA.FORTRAN>, and
909 performing some hand crafting of the result.
911 Information on B<PROMULA.FORTRAN> can be obtained from:
913 PROMULA Development Corporation
914 3620 N. High Street, Suite 301
915 Columbus, Ohio 43214
916 (614) 263-5454
918 F<wrapper.c> was created by using B<SWIG>, and performing some
919 modification of the result. B<SWIG> also produced the skeleton of
920 F<Random.pm>.
922 Information on B<SWIG> can be obtained from:
924 http://www.cs.utah.edu/~beazley/SWIG
928 ftp://ftp.cs.utah.edu/pub/beazley/SWIG
930 =head1 SOURCES
932 The following routines, which were written by others and lightly
933 modified for consistency in packaging, are included in B<randlib>.
935 =over 4
937 =item Bottom Level Routines
939 These routines are a transliteration of the B<Pascal> in the reference
940 to B<Fortran>, and thence to B<C>.
942 L'Ecuyer, P., and Cote, S. "Implementing a Random Number Package with
943 Splitting Facilities." ACM Transactions on Mathematical Software,
944 17:98-111 (1991).
946 =item Exponential
948 This code was obtained from Netlib.
950 Ahrens, J. H., and Dieter, U. "Computer Methods for Sampling from the
951 Exponential and Normal Distributions." Comm. ACM, 15,10 (Oct. 1972),
952 873-882.
954 =item Gamma
956 (Case R >= 1.0)
958 Ahrens, J. H., and Dieter, U. "Generating Gamma Variates by a Modified
959 Rejection Technique." Comm. ACM, 25,1 (Jan. 1982), 47-54.
960 Algorithm GD
962 (Case 0.0 <= R <= 1.0)
964 Ahrens, J. H., and Dieter, U. "Computer Methods for Sampling from
965 Gamma, Beta, Poisson and Binomial Distributions." Computing, 12 (1974),
966 223-246. Adaptation of algorithm GS.
968 =item Normal
970 This code was obtained from netlib.
972 Ahrens, J. H., and Dieter, U. "Extensions of Forsythe's Method for
973 Random Sampling from the Normal Distribution." Math. Comput., 27,124
974 (Oct. 1973), 927-937.
976 =item Binomial
978 This code was kindly sent to Dr. Brown by Dr. Kachitvichyanukul.
980 Kachitvichyanukul, V., and Schmeiser, B. W. "Binomial Random Variate
981 Generation." Comm. ACM, 31, 2 (Feb. 1988), 216.
983 =item Poisson
985 This code was obtained from netlib.
987 Ahrens, J. H., and Dieter, U. "Computer Generation of Poisson Deviates
988 from Modified Normal Distributions." ACM Trans. Math. Software, 8, 2
989 (June 1982), 163-179.
991 =item Beta
993 This code was written by us following the recipe in the following.
995 Cheng, R. C. H. "Generating Beta Variables with Nonintegral Shape
996 Parameters." Comm. ACM, 21:317-322 (1978). (Algorithms BB and BC)
998 =item Linpack
1000 Routines C<SPOFA> and C<SDOT> are used to perform the Cholesky
1001 decomposition of the covariance matrix in C<SETGMN> (used for the
1002 generation of multivariate normal deviates).
1004 Dongarra, J. J., Moler, C. B., Bunch, J. R., and Stewart, G. W.
1005 Linpack User's Guide. SIAM Press, Philadelphia. (1979)
1007 =item Multinomial
1009 The algorithm is from page 559 of Devroye, Luc Non-Uniform Random
1010 Variate Generation. New York: Springer-Verlag, 1986.
1012 =item Negative Binomial
1014 The algorithm is from page 480 of Devroye, Luc Non-Uniform Random
1015 Variate Generation. New York: Springer-Verlag, 1986.
1017 =back
1019 =head1 VERSION
1021 This POD documents B<Math::Random> version 0.66.
1023 =head1 AUTHORS
1025 =over 4
1027 =item *
1029 B<Math::Random> (the B<Perl> port of B<Randlib>) was put together by
1030 John Venier and Barry W. Brown with help from B<SWIG>. For version
1031 0.61, Geoffrey Rommel made various cosmetic changes. Version 0.64 uses
1032 plain vanilla XS rather than SWIG.
1034 =item *
1036 B<randlib> was compiled and written by Barry W. Brown, James Lovato,
1037 Kathy Russell, and John Venier.
1039 =item *
1041 Correspondence regarding B<Math::Random> or B<randlib> should be
1042 addressed to John Venier by email to
1044 venier@odin.mdacc.tmc.edu
1046 =item *
1048 Our address is:
1050 Department of Biomathematics, Box 237
1051 The University of Texas, M.D. Anderson Cancer Center
1052 1515 Holcombe Boulevard
1053 Houston, TX 77030
1055 =item *
1057 Geoffrey Rommel may be reached at grommel@cpan.org.
1059 =back
1061 =head1 LEGALITIES
1063 =over 4
1065 =item *
1067 The programs in the B<Perl> code distributed with B<Math::Random> and
1068 in the B<C> code F<helper.c>, as well as the documentation, are
1069 copyright by John Venier and Barry W. Brown for the University of
1070 Texas M. D. Anderson Cancer Center in 1997. They may be distributed
1071 and used under the same conditions as B<Perl>.
1073 =item *
1075 F<randlib.c>, F<com.c>, and F<randlib.h> are from B<randlib> (See
1076 L<"RANDLIB">) and are distributed with the following legalities:
1078 Code that appeared in an ACM publication is subject to their
1079 algorithms policy:
1081 Submittal of an algorithm for publication in one of the ACM
1082 Transactions implies that unrestricted use of the algorithm within a
1083 computer is permissible. General permission to copy and distribute
1084 the algorithm without fee is granted provided that the copies are not
1085 made or distributed for direct commercial advantage. The ACM
1086 copyright notice and the title of the publication and its date appear,
1087 and notice is given that copying is by permission of the Association
1088 for Computing Machinery. To copy otherwise, or to republish, requires
1089 a fee and/or specific permission.
1091 Krogh, F. "Algorithms Policy." ACM Tran. Math. Softw. 13 (1987),
1092 183-186.
1094 Note, however, that only the particular expression of an algorithm can
1095 be copyrighted, not the algorithm per se; see 17 USC 102.
1097 We place the Randlib code that we have written in the public domain.
1099 =item *
1101 B<Math::Randlib> and B<randlib> are distributed with B<NO WARRANTY>.
1102 See L<"NO WARRANTY">.
1104 =back
1106 =head1 NO WARRANTY
1108 WE PROVIDE ABSOLUTELY NO WARRANTY OF ANY KIND EITHER EXPRESS OR
1109 IMPLIED, INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1110 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
1111 AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD
1112 THIS PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
1113 SERVICING, REPAIR OR CORRECTION.
1115 IN NO EVENT SHALL THE UNIVERSITY OF TEXAS OR ANY OF ITS COMPONENT
1116 INSTITUTIONS INCLUDING M. D. ANDERSON HOSPITAL BE LIABLE TO YOU FOR
1117 DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
1118 INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
1119 INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA OR
1120 ITS ANALYSIS BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD
1121 PARTIES FROM) THE PROGRAM.
1123 (Above NO WARRANTY modified from the GNU NO WARRANTY statement.)
1125 =cut