Random Number Generation
Introduction | Methods |
Random Generation Functions | Statistical Distributions |
Seeding and Localization | References |



Random Numbers
RandomReal[] | give a pseudorandom real number in the range 0 to 1 |
RandomReal[{xmin,xmax}] | give a pseudorandom real number in the range xmin to xmax |
RandomReal[xmax] | give a pseudorandom real number in the range 0 to xmax |
RandomReal[domain,n] | give a list of n pseudorandom reals |
RandomReal[domain,{n1,n2,…}] | give an n1×n2×… array of pseudorandom reals |
RandomInteger[{imin,imax}] | give a pseudorandom integer in the range {imin,…,imax} |
RandomInteger[imax] | give a pseudorandom integer in the range {0,…,imax} |
RandomInteger[] | pseudorandomly give 0 or 1 with probability ![]() |
RandomInteger[domain,n] | give a list of n pseudorandom integers |
RandomInteger[domain,{n1,n2,…}] | give an n1×n2×… array of pseudorandom integers |
RandomComplex[] | give a pseudorandom complex number in the unit square |
RandomComplex[{zmin,zmax}] | give a pseudorandom complex number in the rectangle bounded by zmin and zmax |
RandomComplex[zmax] | give a pseudorandom complex number in the rectangle bounded by 0 and zmax |
RandomComplex[domain,n] | give a list of n pseudorandom complex numbers |
RandomComplex[domain,{n1,n2,…}] | give an n1×n2×… array of pseudorandom complex numbers |
RandomVariate[dist] | give a pseudorandom value from the distribution dist |
RandomVariate[dist,n] | give a list of n pseudorandom values from dist |
RandomVariate[dist,{n1,n2,…}] | give an n1×n2×… array of pseudorandom values from dist |
RandomPrime[{imin,imax}] | give a pseudorandom prime in the range {imin,…,imax} |
RandomPrime[imax] | give a pseudorandom prime in the range 2 to imax |
RandomPrime[domain,n] | give a list of n pseudorandom primes |
RandomPrime[domain,{n1,n2,…}] | give an n1×n2×… array of pseudorandom primes |










Arbitrary-Precision Reals and Complexes
option name | default value | |
WorkingPrecision | MachinePrecision | precision of the arithmetic to use in calculations |


Random Elements
RandomChoice[{e1,e2,…}] | give a pseudorandom choice of one of the ei |
RandomChoice[list,n] | give a list of n pseudorandom choices from list |
RandomChoice[list,{n1,n2,…}] | give n1×n2×… pseudorandom choices from list |
RandomChoice[{w1,w2,…}->{e1,e2,…}] | |
give a pseudorandom choice weighted by the wi | |
RandomChoice[wlist->elist,n] | give a list of n weighted choices |
RandomChoice[wlist->elist,{n1,n2,…}] | |
give an array of n1×n2×… array of weighted choices |
RandomSample[{e1,e2,…},n] | give a pseudorandom sample of n of the ei |
RandomSample[{w1,w2,…}->{e1,e2,…},n] | |
give a pseudorandom sample of n of the ei chosen using weights wi | |
RandomSample[{e1,e2,…}] | give a pseudorandom permutation of the ei |
RandomSample[wlist->elist] | give a pseudorandom permutation of elist using initial weights wlist |

BlockRandom[expr] | evaluate expr with all pseudorandom generators localized |
SeedRandom[n] | reset the pseudorandom generator using n as a seed |
SeedRandom[] | reset the generator using as a seed the time of day and certain attributes of the current Wolfram Language session |
SeedRandom and BlockRandom in Parallel Computations
command | serial | parallel |
SeedRandom[seed] | seed all current serial random generators with seed and the parallel generators with with seed + i, where i is the index for the parallel thread | seed only the random generator for the current thread with seed |
SeedRandom[seed,Method->"ParallelGenerator"] | seed the parallel generators with seed + i, where i is the index for the parallel thread | no effect |
SeedRandom[Method->method] | change the method for the serial random generator to method | change the method for only the random generator for the current thread to method |
BlockRandom[expr] | evaluate expr with all pseudorandom generators localized | evaluate expr with only the pseudorandom generator for the current thread localized |




"Congruential" |
linear congruential generator (low
‐
quality randomness)
|
"ExtendedCA" |
extended cellular automaton generator (default)
|
"Legacy" | default generators prior to version 6.0 |
"MersenneTwister" | Mersenne Twister shift register generator |
"MKL" |
Intel MKL generator (Intel
‐
based systems)
|
"ParallelGenerator" | used for initializing and seeding generators for parallel computations. |
"ParallelMersenneTwister" | set of 1024 Mersenne Twister generators of period ![]() |
"Rule30CA" | Wolfram rule 30 generator |
Congruential








option name | default value | |
"Bits" | Automatic | specify range of bits to use for numbers constructed from bits |
"Multiplier" | 1283839219676404755 | multiplier value |
"Increment" | 0 | increment value |
"Modulus" | 2305843009213693951 | modulus value |
"ConvertToRealsDirectly" | True | whether reals should be constructed directly from the congruence relation |



ExtendedCA
option name | default value | |
"Size" | 80 | state vector size as a multiplier of 64 |
"Skip" | 4 | number of cells to skip |
"Start" | 0 | which cell to start from |

Legacy
MersenneTwister

Reference implementation
genrand64_int64 ()
#include <stdio.h>
#include "mt64.h"
int main(void) {
int i;
unsigned long long init[1], length=1;
init[0]=1ULL; /*SeedRandom[1, Method -> "MersenneTwister"];*/
init_by_array64(init, length);
for (i=0; i<5; i++) {
printf("%20llu\n", genrand64_int64());
}
return 0;
}
/*Output*/
7259937129391483703
7973299316636211948
16865006314979686608
5442441613857606270
14480929463982189498
Wolfram Language
Reals
genrand64_real1()
#include <stdio.h>
#include "mt64.h"
int main(void) {
int i;
unsigned long long init[1], length=1;
init[0]=1ULL; /*SeedRandom[1, Method -> "MersenneTwister"];*/
init_by_array64(init, length);
for (i=0; i<3; i++) {
printf("%16.15f\n", genrand64_real1());
}
return 0;
}
/*Output*/
0.393561980389721
0.432233422048709
0.914253824284137
Wolfram Language
MKL
"MCG31" | 31-bit multiplicative congruential generator |
"MCG59" | 59-bit multiplicative congruential generator |
"MRG32K3A" | combined multiple recursive generators with two components of order 3 |
"MersenneTwister" | Mersenne Twister shift register generator |
"R250" | generalized feedback shift register generator |
"WichmannHill" | Wichmann–Hill combined multiplicative congruential generators |
"Niederreiter" | Niederreiter low-discrepancy sequence |
"Sobol" | Sobol low-discrepancy sequence |
Rule30CA

ParallelMersenneTwister

ParallelGenerator
"ParallelMersenneTwister" | parallel Mersenne twister generators with period ![]() |
"ExtendedCA" | extended CA generators with different starting positions |
f | generator f[i] used for the i th thread |
"Default" | restores the default method |
Defining Your Own Generator
GeneratesBitsQ | set to True if the method generates bits |
GeneratesIntegersQ | set to True if the method generates integers for a given range |
GeneratesRealsQ | set to True if the method generates reals for a given range and precision |


Example: Multiplicative Congruential Generator

Example: Blum–Blum–Shub Generator




RandomVariate[dist] | give a random number from the continuous distribution dist |
RandomVariate[dist,n] | give a list of n pseudorandom reals from dist |
RandomVariate[dist,{n1,n2,…}] | give an n1×n2×… array of pseudorandom reals from dist |
Continuous Distributions





























Discrete Distributions








Defining Distributional Generators
Random`DistributionVector[expr,n,prec] | |
defines rules for generating n observations from expr with precision prec |


Example: Normal Distribution by Inversion


Example: Uniform Distribution on a Disk


Example: Gibbs Sampler
[1] Geman, S. and D. Geman. "Stochastic Relaxation, Gibbs Distributions, and the Bayesian Restoration of Images." IEEE Transactions on Pattern Analysis and Machine Intelligence 6, no. 6 (1984): 721–741.
[2] Casella, G. and E. I. George. "Explaining the Gibbs Sampler." The American Statistician 46, no. 3 (1992): 167–174.
[3] Matsumoto, M. and T. Nishimura. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudorandom Number Generator." ACM Transactions on Modeling and Computer Simulation 8, no. 1 (1998): 3–30.
[4] Nishimura, T. "Tables of 64-Bit Mersenne Twisters." ACM Transactions on Modeling and Computer Simulation 10, no. 4 (2000): 348–357.
[5] Junod, P. "Cryptographic Secure Pseudo-Random Bits Generation: The Blum–Blum–Shub Generator." August 1999. http://crypto.junod.info/bbs.pdf
[6] Gentle, J. E. Random Number Generation and Monte Carlo Methods, 2nd ed. Springer-Verlag, 2003.
[7] Johnson, N. L., S. Kotz, and N. Balakrishnan. Continuous Univariate Distributions, Volume 2, 2nd ed. John Wiley & Sons, 1995.
[8] Smith, W. B. and R. R. Hocking. "Algorithm AS 53: Wishart Variate Generator." Applied Statistics 21, no. 3 (1972): 341–345.
[9] Cheng, R. C. H. and G. M. Feast. "Some Simple Gamma Variate Generators." Applied Statistics 28, no. 3 (1979): 290–295.
[10] Johnson, M. E. Multivariate Statistical Simulation. John Wiley & Sons, 1987.
[11] Jöhnk, M. D. "Erzeugung von Betaverteilten und Gammaverteilten Zufallszahlen." Metrika 8 (1964): 5–15.
[12] Cheng, R. C. H. "Generating Beta Variables with Nonintegral Shape Parameters." Communications of the ACM 21, no. 4 (1978): 317–322.
[13] Atkinson, A. C. "A Family of Switching Algorithms for the Computer Generation of Beta Random Variables." Biometrika 66, no. 1 (1979): 141–145.
[14] Bailey, R. W. "Polar Generation of Random Variates with the t-Distribution." Mathematics of Computation 62, no. 206 (1994): 779–781.
[15] Devroye, L. Non-Uniform Random Variate Generation. Springer-Verlag, 1986.
[16] Kachitvichyanukul, V. and B. W. Schmeiser. "Binomial Random Variate Generation." Communications of the ACM 31, no. 2 (1988): 216–223.
[17] Kachitvichyanukul, V. and B. W. Schmeiser. "Computer Generation of Hypergeometric Random Variates." Journal of Statistical Computation and Simulation 22, no. 2 (1985): 127–145.
[18] Ahrens, J. H. and U. Dieter. "Computer Generation of Poisson Deviates from Modified Normal Distributions." ACM Transactions on Mathematical Software 8, no. 2 (1982): 163–179.
[19] Matsumoto, M. and T. Nishimura. "Dynamic Creation of Pseudorandom Number Generators." In Proceedings of the Third International Conference on Monte Carlo and Quasi-Monte Carlo Methods in Scientific Computing: Monte Carlo and Quasi‐Monte Carlo Methods 1998, 56–69, 2000.