Work with Random Generators - Maple Help

Home : Support : Online Help : Getting Started : How Do I... : Work with Random Generators

How Do I

 Work with Random Generators?

Maple provides different methods for efficiently generating random numbers (floats, integers, and rational numbers), rtables (Arrays, Matrices, and Vectors) with random entries, graphs, logic, and more. This page suggests the best commands available for such purposes.

To interact with the examples shown on this page, open it as worksheet and execute the steps.

Integer

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

rand - is the simplest command to generate random integers sampled uniformly from the range $0$ to ${10}^{12}-1$ and has a period of ${2}^{19937}-1$. It calls RandomTools[MersenneTwister][GenerateInteger] or RandomTools[MersenneTwister][NewGenerator] depending on whether or not a number or procedure is to be returned.

In order to vary the numbers generated, you can call randomize(n) where n (a positive integer) will set the initial state. With no arguments, randomize() will use a number based on the system clock as the initial state.

 > $\mathrm{restart}$

Returns a single random integer:

 > $\mathrm{rand}\left(\right)$
 ${959740139875}$ (1.1)

A procedure that can generate integers from a specified range:

 > $R≔\mathrm{rand}\left(100..200\right):$
 > $R\left(\right)$
 ${192}$ (1.2)
 > $R\left(\right)$
 ${144}$ (1.3)

Returns a sequence of random numbers using:

 > $\mathrm{seq}\left(R\left(\right),i=1..5\right)$
 ${144}{,}{195}{,}{105}{,}{197}{,}{158}$ (1.4)



Other available commands to generate random integers:

 • RandomTools[MersenneTwister][GenerateInteger] - uses the MersenneTwister algorithm that is implemented in the kernel. The length of the output depends on the value of the range option, that is [0, value), otherwise the default is ${10}^{12}$.
 • RandomTools[MersenneTwister][NewGenerator] - the procedure used to return numerous integers at once.
 • RandomTools[MersenneTwister][GenerateInteger32] - uses the MersenneTwister algorithm to generate a signed 32-bit random integer.
 • RandomTools[MersenneTwister][GenerateUnsignedInt32] - uses the MersenneTwister algorithm to generate an unsigned 32-bit random integer.
 • RandomTools[LinearCongruence][GenerateInteger] - uses the LinearCongruence algorithm and is ideal for educational purposes. The length of output depends on the value of the range option, that is [0, value), otherwise the default is ${10}^{12}$.
 • RandomTools[Generate] - this command recognizes several flavors that return integers. The following is a list of these flavors that can be used depending on the type of output desired: integer, posint, negint, nonnegint, nonposint, and nonzeroint.
 •

Return to the top of the page

Float

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

generates a pseudo-random float uniformly distributed in [0,1). It uses the MersenneTwister algorithm that is implemented in the kernel and is extremely fast.

The MersenneTwister algorithm is intended to be used as a general purpose random number generator. It is good for randomized algorithm and generating random data and has a period of ${2}^{19937}-1$.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{RandomTools}\left[\mathrm{MersenneTwister}\right]\right):$
 > $\mathrm{GenerateFloat}\left(\right)$
 ${0.0581869302}$ (2.1)

Uses the digits=integer option to specify how many random digits should be returned:

 > $\mathrm{GenerateFloat}\left(\mathrm{digits}=5\right)$
 ${0.13612}$ (2.2)





Other available commands to generate random floats are:

 • RandomTools[MersenneTwister][GenerateFloat64] - uses the MersenneTwister algorithm to generate a pseudo-random float[8] value that is uniformly distributed in [0,1).
 • Random[Generate(float)], where float is one of the many flavors recognized by the Generate command.

Return to the top of the page

Rational

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

RandomTools[Generate](rational) - uses the flavor rational  for the Generate command to output rational numbers.

Generate creates a particular random object that is determined by the flavor and data structures of the parameters chosen or combinations of them. The underlying procedure of this command is based on the Mersenne Twister algorithm that has a period of ${2}^{19937}-1$.

For a list of Maple types, flavor templates, and data structures that are recognized by Generate, see RandomTools[Generate].

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{RandomTools}\right):$
 > $\mathrm{Generate}\left(\mathrm{rational}\right)$
 ${-}\frac{{104281139459}}{{499999999994}}$ (3.1)
 >
 $\frac{{7799}}{{26500}}$ (3.2)
 >
 $\left[\begin{array}{ccc}{-}\frac{{1}}{{3}}{}{x}{-}\frac{{15}}{{16}}& {-}\frac{{3}}{{4}}{}{x}{-}\frac{{3}}{{16}}& \frac{{11}}{{12}}{}{x}{-}\frac{{9}}{{16}}\\ \frac{{1}}{{6}}{}{x}{-}\frac{{9}}{{16}}& {-}\frac{{7}}{{8}}{}{x}{-}\frac{{1}}{{8}}& \frac{{23}}{{24}}{}{x}{-}\frac{{1}}{{16}}\\ {-}\frac{{1}}{{2}}{}{x}{+}\frac{{3}}{{16}}& \frac{{2}}{{3}}{}{x}{+}\frac{{5}}{{8}}& {-}\frac{{7}}{{8}}{}{x}{+}\frac{{1}}{{16}}\end{array}\right]$ (3.3)



RandomTools[Generate] recognizes several other flavors that return rational numbers. The following is a list of these flavors that can be used depending on the type of output desired:

Return to the top of the page

Random Data Sample

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

Statistics[Sample] - this function takes a probability distribution (see Statistics[Distributions]), a random variable, or an algebraic expression involving random variables (see Statistics[RandomVariable]) and creates a Vector, Matrix, or Array containing the sample values. Outputs of the Sample command are random floats by default.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{Statistics}\right):$
 > $\mathrm{Sample}\left(\mathrm{Normal}\left(0,1\right),3\right)$
 $\left[\begin{array}{ccc}{1.21524996695803}& {-}{0.916311250504808}& {-}{0.760539518007130}\end{array}\right]$ (4.1)
 > $\mathrm{Sample}\left(\mathrm{Uniform}\left(0,1\right),1\right)\left[1\right]$
 ${0.913375856139019}$ (4.2)



Other available functions to generate random data sample (float):

 • This function is also featured in the Students package under Student[Statistics][Sample].
 • RandomTools[Generate(distribution)], where distribution is one of the many flavors recognized by the Generate command.

Return to the top of the page

Polynomials

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

randpoly - a random polynomial generator, useful for generating test problems for debugging, testing, and demonstration purposes. The default range of the coefficients generated is $-99..99$.

 > $\mathrm{restart}$
 > $\mathrm{randpoly}\left(x\right)$
 ${-}{7}{}{{x}}^{{5}}{+}{22}{}{{x}}^{{4}}{-}{55}{}{{x}}^{{3}}{-}{94}{}{{x}}^{{2}}{+}{87}{}{x}{-}{56}$ (5.1)

Random homogeneous polynomial:

 > $\mathrm{randpoly}\left(\left[x,y\right]\right)$
 ${-}{75}{}{{x}}^{{4}}{}{y}{-}{17}{}{{x}}^{{2}}{}{y}{+}{80}{}{{x}}^{{2}}{-}{44}{}{x}{}{y}{+}{71}{}{{y}}^{{2}}{-}{82}{}{x}$ (5.2)

Use various options, such as coeffs, expons, terms, degree, dense, and homogeneous in order to return appropriate outputs. For example:

 >
 ${-}{23}{}{{x}}^{{4}}{+}{87}{}{{x}}^{{3}}{}{y}{+}{29}{}{{x}}^{{2}}{}{{y}}^{{2}}{+}{10}{}{x}{}{{y}}^{{3}}{+}{95}{}{{y}}^{{4}}{+}{44}{}{{x}}^{{3}}{+}{98}{}{{x}}^{{2}}{}{y}{-}{61}{}{x}{}{{y}}^{{2}}{+}{11}{}{{y}}^{{3}}{-}{23}{}{{x}}^{{2}}{-}{8}{}{x}{}{y}{-}{49}{}{{y}}^{{2}}{-}{29}{}{x}{-}{47}{}{y}{+}{40}$ (5.3)
 > 



Other available commands to generate random polynomials are:

 • Randpoly - generates a random polynomial over a finite field.
 • Randprime - generates a random, monic, prime polynomial over a finite field.
 • RandomTools[Generate(polynom)], where polynom is one of the many flavors recognized by the Generate command.

Return to the top of the page

Array (Integer Entries)

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

Array - use the optional parameter random to generate Arrays with random integer entries. The default range for random is all possible integers.

 > $\mathrm{restart}$

An Array of five integers from a given range:

 > $\mathrm{Array}\left(1..5,\mathrm{random}\left(0..10\right)\right)$
 $\left[\begin{array}{ccccc}{4}& {3}& {6}& {9}& {5}\end{array}\right]$ (6.1)

Create a random Array of $n$-byte hardware integer, where $n$ is one of $1$, $2$, $4$, or $8$:

 >
 $\left[\begin{array}{ccccc}{-}{82}& {122}& {49}& {-}{115}& {-}{36}\end{array}\right]$ (6.2)
 > $\mathrm{Array}\left(\mathrm{antisymmetric},1..2,1..2,\mathrm{random}\left(\right)\right)$
 $\left[\begin{array}{rr}{0}& {51990904054251702}\\ {-}{51990904054251702}& {0}\end{array}\right]$ (6.3)

Alternatively, use rtable to generate random Arrays with integer, float, and complex entries by specifying the datatype as integer[n], float[n], and complex[n], respectively.

Return to the top of the page

Array (Float Entries)

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

Array - use the optional parameter frandom to generate Arrays with random floating-point entries. The default range for frandom is $0..1$.

 > $\mathrm{restart}$
 > $\mathrm{Array}\left(1..5,\mathrm{frandom}\left(\right)\right)$
 $\left[\begin{array}{ccccc}{0.240075952380469526}& {0.990109957300138177}& {0.948925087978892745}& {0.199872870242786216}& {0.140143840101215611}\end{array}\right]$ (7.1)

range=0..0.5:

 >
 $\left[\begin{array}{ccccc}{0.270069000502103307}& {0.238319036393397632}& {0.389844684254544604}& {0.194307586634438756}& {0.00826029311923809351}\end{array}\right]$ (7.2)
 > 

Alternatively, use rtable to generate random Arrays with integer, float, and complex entries by specifying the datatype as integer[n], float[n], and complex[n], respectively.

Return to the top of the page

Array (From a Distribution)

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

ArrayTools[RandomArray] - the RandomArray(m, n, o, ...) and RandomArray(m, n, o, ..., distribution = uniform) calls randomly generate an m-by-n-by-o-by-... array of values (floats) drawn from a uniform distribution. RandomArray(m, n, o, ..., distribution = normal) call draws values (floats) from a normal distribution to create an m-by-n-by-o-by-... array.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{ArrayTools}\right):$
 > $\mathrm{RandomArray}\left(\right)$
 ${0.2342493224}$ (8.1)
 > $T≔\mathrm{RandomArray}\left(2,4,6,7\right)$
 ${T}{:=}\left[\begin{array}{c}{\mathrm{1..2 x 1..4 x 1..6 x 1..7}}{\mathrm{Array}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (8.2)

Number of elements in the array:

 > $\mathrm{numelems}\left(T\right)$
 ${336}$ (8.3)
 > $\mathrm{RandomArray}\left(1,2,\mathrm{distribution}=\mathrm{normal}\right)$
 $\left[\begin{array}{cc}{0.492568377027658}& {-}{0.0395071892741721}\end{array}\right]$ (8.4)
 >
 ${A}{:=}\left[\begin{array}{c}{\mathrm{1..2 x 1..1 x 1..3 x 1..6 x 1..4}}{\mathrm{Array}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (8.5)
 > $\mathrm{numelems}\left(A\right)$
 ${144}$ (8.6)



Other available commands to generate random arrays are:

 • Statistics[Sample] - generates random sample, where the parameter, rng, is a range or a list of ranges determining the dimensions of an Array. This Array will be created, filled with the sample values, and returned.
 • Alternatively, this function is also featured in the Students package under Student[Statistics][Sample].

Return to the top of the page

Matrix (Integer Entries)

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

LinearAlgebra[RandomMatrix] - this function returns an $m×n$ random matrix where all the entries are integers in the range of $-99..99$ and has a period of ${2}^{19937}-1$.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{LinearAlgebra}\right):$

Create an $m×n$ random matrix:

 > $\mathrm{RandomMatrix}\left(3,2\right)$
 $\left[\begin{array}{rr}{-}{94}& {-}{53}\\ {-}{7}& {21}\\ {12}& {-}{25}\end{array}\right]$ (9.1)

 > $\mathrm{RandomMatrix}\left(3,3,\mathrm{shape}=\mathrm{diagonal}\right)$
 $\left[\begin{array}{rrr}{-}{2}& {0}& {0}\\ {0}& {-}{72}& {0}\\ {0}& {0}& {-}{76}\end{array}\right]$ (9.2)

Where shape=diagonal is one of the built-in indexing functions for Matrices. See Matrix for details.

Other available commands to construct a Matrix with random integers are:

 • This function is also featured in the Students package under Student[LinearAlgebra][RandomMatrix].
 • RandomTools[Generate(Matrix)], where Matrix is one of the many flavors recognized by the Generate command. The default flavor of each entry is integer.

Return to the top of the page

Matrix (Float Entries)

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

ArrayTools[RandomArray] - calls to return an $m×n$ matrix where entries are random floats.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{ArrayTools}\right):$

Generate a 3-by-3 matrix of values:

 > $\mathrm{RandomArray}\left(3\right)$
 $\left[\begin{array}{ccc}{0.957506835434298}& {0.0975404049994095}& {0.126986816293506}\\ {0.546881519204984}& {0.632359246225410}& {0.905791937075619}\\ {0.278498218867048}& {0.913375856139019}& {0.814723686393179}\end{array}\right]$ (10.1)

The above is equivalent to calling: $\mathrm{RandomArray}\left(3,\mathrm{distribution}=\mathrm{uniform}\right)$.

Generate a 3-by-2 Matrix using normal distribution:

 > $\mathrm{RandomArray}\left(3,2,\mathrm{distribtion}=\mathrm{normal}\right)$
 $\left[\begin{array}{cc}{0.0975404049994095}& {0.126986816293506}\\ {0.632359246225410}& {0.905791937075619}\\ {0.913375856139019}& {0.814723686393179}\end{array}\right]$ (10.2)



Other available commands to construct a Matrix with random floats are:

 •
 • RandomTools[Generate] - by combining flavors Matrix and distribution.

Return to the top of the page

Matrix (From a Given Distribution)

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

Statistics[Sample] - this function takes a probability distribution (see Statistics[Distributions]), a random variable, or an algebraic expression involving random variables (see Statistics[RandomVariable]) and creates a Vector, Matrix, or Array containing the sample values. Outputs of the Sample command are random floats by default.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{Statistics}\right):$

Generate a 3-by-3 Matrix using Normal distribution:

 > $M≔\mathrm{Sample}\left(\mathrm{Normal}\left(0,1\right),\left[3,3\right]\right)$
 ${M}{:=}\left[\begin{array}{ccc}{-}{1.07242412799827}& {0.214466745245291}& {-}{1.63485675434390}\\ {-}{0.329077870547065}& {-}{0.0254281280423846}& {1.57117217175430}\\ {-}{0.617091936909790}& {1.72882128417783}& {0.170358421410408}\end{array}\right]$ (11.1)

Other available commands to construct a Matrix with random floats are:

 • This function is also featured in the Students package under Student[Statistics][Sample].
 • RandomTools[Generate] - by combining flavors Matrix and distribution. (CPU time=5.16 ms)

Return to the top of the page

Vector

Recommended Command

Examples

Notes (links to other commands with same functionality) / Other Available Commands

LinearAlgebra[RandomVector] - constructs a random Vector. The entries returned are integers in the range of $-99..99$ and has a period of ${2}^{19937}-1$.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{LinearAlgebra}\right):$

Random vector with integer entries:

 > $\mathrm{RandomVector}\left(3,\mathrm{generator}=1..9\right)$
 $\left[\begin{array}{r}{1}\\ {7}\\ {1}\end{array}\right]$ (12.1)
 > $\mathrm{RandomVector}\left[\mathrm{row}\right]\left(3,\mathrm{generator}=\mathrm{rand}\left(1..10\right)\right)$
 $\left[\begin{array}{ccc}{10}& {6}& {2}\end{array}\right]$ (12.2)

Random vector with float entries:

 > $\mathrm{RandomVector}\left(3,\mathrm{generator}=0.1..0.9\right)$
 $\left[\begin{array}{c}{0.694505974499932899}\\ {0.706192104462666781}\\ {0.642988123886218732}\end{array}\right]$ (12.3)
 • This function is also featured in the Students package under Student[LinearAlgebra][RandomVector].
 • RandomTools[Generate(Vector)], where Vector is one of the many flavors recognized by the Generate command. The default flavor of each entry is integer.

Return to the top of the page

List

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

RandomTools[Generate(list)] - uses the flavor list  for the Generate command to return a list of random objects, which are specified by other flavors.

Generate creates a particular random object that is determined by the flavor and data structures of the parameters chosen or combinations of them. The underlying procedure of this command is based on the Mersenne Twister algorithm that has a period of ${2}^{19937}-1$.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{RandomTools}\right):$

List of random integers:

 > $\mathrm{Generate}\left(\mathrm{list}\left(\mathrm{integer},3\right)\right)$
 $\left[{-}{104281139460}{,}{-}{306860183579}{,}{-}{477575829529}\right]$ (13.1)

List of random floats:

 > $\mathrm{Generate}\left(\mathrm{list}\left(\mathrm{float},2\right)\right)$
 $\left[{8.344539878}{}{{10}}^{{-7}}{,}{8.412421905}{}{{10}}^{{-10}}\right]$ (13.2)

List of Matrices with integer entries:

 > $\mathrm{Generate}\left(\mathrm{list}\left('\mathrm{Matrix}'\left(\mathrm{integer},2,2\right),2\right)\right)$
 $\left[\left[\begin{array}{rr}{-}{348256258834}& {-}{181644806914}\\ {482438952754}& {-}{383395241439}\end{array}\right]{,}\left[\begin{array}{rr}{-}{205804620411}& {-}{390317514418}\\ {461153517356}& {-}{245484114740}\end{array}\right]\right]$ (13.3)

Use flav=listlist to generate a list of lists:

 > $\mathrm{Generate}\left(\mathrm{listlist}\left(\mathrm{rational},2\right)\right)$
 $\left[\left[\frac{{236602622351}}{{499999999994}}{,}{-}\frac{{170155408191}}{{499999999994}}\right]{,}\left[\frac{{10521097205}}{{45454545454}}{,}\frac{{347018765395}}{{499999999994}}\right]\right]$ (13.4)
 > $\mathrm{Generate}\left(\mathrm{listlist}\left(\mathrm{integer}\left(\mathrm{range}=1..10\right),3,2\right)\right)$
 $\left[\left[{7}{,}{7}\right]{,}\left[{3}{,}{3}\right]{,}\left[{5}{,}{3}\right]\right]$ (13.5)



For a complete list of Maple types, flavors, templates, and data structures that can be combined with flav=list, see RandomTools[Generate].

Return to the top of the page

Set

Recommended Command

Examples

Notes (links to other commands with the same functionality) / Other Available Commands

RandomTools[Generate(set)] - uses the flavor set to generate a set of random objects.

Generate creates a particular random object that is determined by the flavor and data structures of the parameters chosen or combinations of them. The underlying procedure of this command is based on the Mersenne Twister algorithm that has a period of ${2}^{19937}-1$.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{RandomTools}\right):$

Set of random positive integers:

 > $\mathrm{Generate}\left(\mathrm{set}\left(\mathrm{posint},3\right)\right)$
 $\left\{{33467275626}{,}{57646025778}{,}{79394083865}\right\}$ (14.1)

Generate a set of random negative integers:

 > $\mathrm{Generate}\left(\mathrm{set}\left(\mathrm{negint}\left(\mathrm{range}=-10\right),3\right)\right)$
 $\left\{{-}{8}{,}{-}{4}\right\}$ (14.2)

where, n=3 is the maximum number of entries in the set.

Set of Vectors with nonzero rational entries:

 > $\mathrm{Generate}\left(\mathrm{set}\left('\mathrm{Vector}'\left(\mathrm{nonzero}\left(\mathrm{range}=-5..5\right),2\right),2\right)\right)$