Navigation

Operators and Keywords

Function List:

C++ API

rand.cc File Reference

#include <ctime>
#include <string>
#include "f77-fcn.h"
#include "lo-mappers.h"
#include "oct-rand.h"
#include "quit.h"
#include "defun-dld.h"
#include "error.h"
#include "gripes.h"
#include "oct-obj.h"
#include "unwind-prot.h"
#include "utils.h"
#include "ov-re-mat.h"

Include dependency graph for rand.cc:


Functions

 DEFUN_DLD (rand, args,,"-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} rand (@var{x})\n\ @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ @deftypefnx {Loadable Function} {} rand (\"state\", @var{x})\n\ @deftypefnx {Loadable Function} {} rand (\"seed\", @var{x})\n\ Return a matrix with random elements uniformly distributed on the\n\ interval (0, 1). The arguments are handled the same as the arguments\n\ for @code{eye}.\n\ \n\ You can query the state of the random number generator using the\n\ form\n\ \n\ @example\n\ v = rand (\"state\")\n\ @end example\n\ \n\ This returns a column vector @var{v} of length 625. Later, you can\n\ restore the random number generator to the state @var{v}\n\ using the form\n\ \n\ @example\n\ rand (\"state\", v)\n\ @end example\n\ \n\ @noindent\n\ You may also initialize the state vector from an arbitrary vector of\n\ length <= 625 for @var{v}. This new state will be a hash based on the\n\ value of @var{v}, not @var{v} itself.\n\ \n\ By default, the generator is initialized from @code{/dev/urandom} if it is\n\ available, otherwise from cpu time, wall clock time and the current\n\ fraction of a second.\n\ \n\ To compute the pseudo-random sequence, @code{rand} uses the Mersenne\n\ Twister with a period of @math{2^{19937}-1} (See M. Matsumoto and T. Nishimura,\n\ @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator}, ACM Trans. on\n\ Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 1998,\n\ @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).\n\ Do @strong{not} use for cryptography without securely hashing\n\ several returned values together, otherwise the generator state\n\ can be learned after reading 624 consecutive values.\n\ \n\ Older versions of Octave used a different random number generator.\n\ The new generator is used by default\n\ as it is significantly faster than the old generator, and produces\n\ random numbers with a significantly longer cycle time. However, in\n\ some circumstances it might be desirable to obtain the same random\n\ sequences as used by the old generators. To do this the keyword\n\ \"seed\" is used to specify that the old generators should be use,\n\ as in\n\ \n\ @example\n\ rand (\"seed\", val)\n\ @end example\n\ \n\ which sets the seed of the generator to @var{val}. The seed of the\n\ generator can be queried with\n\ \n\ @example\n\ s = rand (\"seed\")\n\ @end example\n\ \n\ However, it should be noted that querying the seed will not cause\n\ @code{rand} to use the old generators, only setting the seed will.\n\ To cause @code{rand} to once again use the new generators, the\n\ keyword \"state\" should be used to reset the state of the @code{rand}.\n\ @seealso{randn, rande, randg, randp}\n\ @end deftypefn")
 DEFUN_DLD (randn, args,,"-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} randn (@var{x})\n\ @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ @deftypefnx {Loadable Function} {} randn (\"state\", @var{x})\n\ @deftypefnx {Loadable Function} {} randn (\"seed\", @var{x})\n\ Return a matrix with normally distributed pseudo-random\n\ elements having zero mean and variance one. The arguments are\n\ handled the same as the arguments for @code{rand}.\n\ \n\ By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique'' to\n\ transform from a uniform to a normal distribution. (G. Marsaglia and\n\ W.W. Tsang, @cite{Ziggurat method for generating random variables},\n\ J. Statistical Software, vol 5, 2000,\n\ @url{http://www.jstatsoft.org/v05/i08/})\n\ \n\ @seealso{rand, rande, randg, randp}\n\ @end deftypefn")
 DEFUN_DLD (rande, args,,"-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} rande (@var{x})\n\ @deftypefnx {Loadable Function} {} rande (@var{n}, @var{m})\n\ @deftypefnx {Loadable Function} {} rande (\"state\", @var{x})\n\ @deftypefnx {Loadable Function} {} rande (\"seed\", @var{x})\n\ Return a matrix with exponentially distributed random elements. The\n\ arguments are handled the same as the arguments for @code{rand}.\n\ \n\ By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique'' to\n\ transform from a uniform to a exponential distribution. (G. Marsaglia and\n\ W.W. Tsang, @cite{Ziggurat method for generating random variables},\n\ J. Statistical Software, vol 5, 2000,\n\ @url{http://www.jstatsoft.org/v05/i08/})\n\ @seealso{rand, randn, randg, randp}\n\ @end deftypefn")
 DEFUN_DLD (randg, args,,"-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} randg (@var{a}, @var{x})\n\ @deftypefnx {Loadable Function} {} randg (@var{a}, @var{n}, @var{m})\n\ @deftypefnx {Loadable Function} {} randg (\"state\", @var{x})\n\ @deftypefnx {Loadable Function} {} randg (\"seed\", @var{x})\n\ Return a matrix with @code{gamma(@var{a},1)} distributed random elements.\n\ The arguments are handled the same as the arguments for @code{rand},\n\ except for the argument @var{a}.\n\ \n\ This can be used to generate many distributions:\n\ \n\ @table @asis\n\ @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0}\n\ @example\n\ r = b * randg (a)\n\ @end example\n\ @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1}\n\ @example\n\ @group\n\ r1 = randg (a, 1)\n\ r = r1 / (r1 + randg (b, 1))\n\ @end group\n\ @end example\n\ @item @code{Erlang (a, n)}\n\ @example\n\ r = a * randg (n)\n\ @end example\n\ @item @code{chisq (df)} for @code{df > 0}\n\ @example\n\ r = 2 * randg (df / 2)\n\ @end example\n\ @item @code{t(df)} for @code{0 < df < inf} (use randn if df is infinite)\n\ @example\n\ r = randn () / sqrt (2 * randg (df / 2) / df)\n\ @end example\n\ @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2}\n\ @example\n\ @group\n\ ## r1 equals 1 if n1 is infinite\n\ r1 = 2 * randg (n1 / 2) / n1\n\ ## r2 equals 1 if n2 is infinite\n\ r2 = 2 * randg (n2 / 2) / n2\n\ r = r1 / r2\n\n\ @end group\n\ @end example\n\ @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1}\n\ @example\n\ r = randp ((1 - p) / p * randg (n))\n\ @end example\n\ @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0}\n\ (use chisq if @code{L = 0})\n\ @example\n\ @group\n\ r = randp (L / 2)\n\ r(r > 0) = 2 * randg (r(r > 0))\n\ r(df > 0) += 2 * randg (df(df > 0)/2)\n\ @end group\n\ @end example\n\ @item @code{Dirichlet (a1, @dots{} ak)}\n\ @example\n\ @group\n\ r = (randg (a1), @dots{}, randg (ak))\n\ r = r / sum (r)\n\ @end group\n\ @end example\n\ @end table\n\ @seealso{rand, randn, rande, randp}\n\ @end deftypefn")
 DEFUN_DLD (randp, args,,"-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} randp (@var{l}, @var{x})\n\ @deftypefnx {Loadable Function} {} randp (@var{l}, @var{n}, @var{m})\n\ @deftypefnx {Loadable Function} {} randp (\"state\", @var{x})\n\ @deftypefnx {Loadable Function} {} randp (\"seed\", @var{x})\n\ Return a matrix with Poisson distributed random elements with mean value parameter given by the first argument, @var{l}. The arguments\n\ are handled the same as the arguments for @code{rand}, except for the\n\ argument @var{l}.\n\ \n\ Five different algorithms are used depending on the range of @var{l}\n\ and whether or not @var{l} is a scalar or a matrix.\n\ \n\ @table @asis\n\ @item For scalar @var{l} <= 12, use direct method.\n\ Press, et al., 'Numerical Recipes in C', Cambridge University Press, 1992.\n\ @item For scalar @var{l} > 12, use rejection method.[1]\n\ Press, et al., 'Numerical Recipes in C', Cambridge University Press, 1992.\n\ @item For matrix @var{l} <= 10, use inversion method.[2]\n\ Stadlober E., et al., WinRand source code, available via FTP.\n\ @item For matrix @var{l} > 10, use patchwork rejection method.\n\ Stadlober E., et al., WinRand source code, available via FTP, or\n\ H. Zechner, 'Efficient sampling from continuous and discrete\n\ unimodal distributions', Doctoral Dissertation, 156pp., Technical\n\ University Graz, Austria, 1994.\n\ @item For @var{l} > 1e8, use normal approximation.\n\ L. Montanet, et al., 'Review of Particle Properties', Physical Review\n\ D 50 p1284, 1994\n\ @end table\n\ @seealso{rand, randn, rande, randg}\n\ @end deftypefn")
 DEFUN_DLD (randperm, args,,"-*- texinfo -*-\n\ @deftypefn {Loadable Function} {} randperm (@var{n})\n\ @deftypefnx {Loadable Function} {} randperm (@var{n}, @var{m})\n\ Return a row vector containing a random permutation of @code{1:@var{n}}.\n\ If @var{m} is supplied, return @var{m} permutations,\n\ one in each row of a NxM matrix. The complexity is O(M*N) in both time and\n\ memory. The randomization is performed using rand().\n\ All permutations are equally likely.\n\ @seealso{perms}\n\ @end deftypefn")

Function Documentation

DEFUN_DLD ( randperm  ,
args   
)

DEFUN_DLD ( randp  ,
args   
)

DEFUN_DLD ( randg  ,
args   
)

DEFUN_DLD ( rande  ,
args   
)

DEFUN_DLD ( randn  ,
args   
)

DEFUN_DLD ( rand  ,
args   
)