Random, RandomSeed | (pseudo-) random number generator |
RngCreate | manipulate random number generators as objects |
RngSeed | manipulate random number generators as objects |
Rng | manipulate random number generators as objects |
RandomIntegerMatrix | generate a matrix of random integers |
RandomIntegerVector | generate a vector of random integers |
RandomPoly | construct a random polynomial |
Random() RandomSeed(init) |
*PARAMS init -- positive integer, initial random seed
The random number generator can be initialized by calling RandomSeed with an integer value. Each seed value will result in the same sequence of pseudo-random numbers.
RngCreate() RngCreate(init) RngCreate(option==value,...) RngSeed(r, init) Rng(r) |
option -- atom, option name
value -- atom, option value
r -- a list, RNG object
RngCreate returns a list which is a well-formed RNG object. Its value should be saved in a variable and used to call Rng and RngSeed.
Rng(r) returns a floating-point random number between 0 and 1 and updates the RNG object r. (Currently, the Gaussian option makes a RNG return a complex random number instead of a real random number.)
RngSeed(r,init) re-initializes the RNG object r with the seed value init. The seed value should be a positive integer.
The RngCreate function accepts several options as arguments. Currently the following options are available:
If the initial seed is not specified, the value of 76544321 will be used.
The gauss option will create a RNG object that generates pairs of Gaussian distributed random numbers as a complex random number. The real and the imaginary parts of this number are independent random numbers taken from a Gaussian (i.e. "normal") distribution with unit variance.
For the Gaussian distribution, the Box-Muller transform method is used. A good description of this method, along with the proof that the method generates normally distributed random numbers, can be found in Knuth, "The Art of Computer Programming", Volume 2 (Seminumerical algorithms, third edition), section 3.4.1
Note that unlike the global Random function, the RNG objects created with RngCreate are independent RNGs and do not affect each other. They generate independent streams of pseudo-random numbers. However, the Random function is slightly faster.
In> r1:=RngCreate(seed=1,dist=gauss) Out> {"GaussianRNGDist","RNGEngine'LCG'2",{1}} In> Rng(r1) Out> Complex(-1.6668466417,0.228904004); In> Rng(r1); Out> Complex(0.0279296109,-0.5382405341); |
In> [r2:=RngCreate(engine=advanced);Rng(r2);] Out> 0.3653615377; |
In> RngSeed(r1, 1) Out> True; In> Rng(r1) Out> Complex(-1.6668466417,0.228904004); |
RandomIntegerMatrix(rows,cols,from,to) |
cols -- number of cols in matrix
from -- lower bound
to -- upper bound
In> PrettyForm( RandomIntegerMatrix(5,5,-2^10,2^10) ) |
/ \ | ( -506 ) ( 749 ) ( -574 ) ( -674 ) ( -106 ) | | | | ( 301 ) ( 151 ) ( -326 ) ( -56 ) ( -277 ) | | | | ( 777 ) ( -761 ) ( -161 ) ( -918 ) ( -417 ) | | | | ( -518 ) ( 127 ) ( 136 ) ( 797 ) ( -406 ) | | | | ( 679 ) ( 854 ) ( -78 ) ( 503 ) ( 772 ) | \ / |
RandomIntegerVector(nr, from, to) |
from -- lower bound
to -- upper bound
In> RandomIntegerVector(4,-3,3) Out> {0,3,2,-2}; |
RandomPoly(var,deg,coefmin,coefmax) |
deg -- degree of resulting univariate polynomial
coefmin -- minimum value for coefficients
coefmax -- maximum value for coefficients
In> RandomPoly(x,3,-10,10) Out> 3*x^3+10*x^2-4*x-6; In> RandomPoly(x,3,-10,10) Out> -2*x^3-8*x^2+8; |