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)
 >



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)$
 > $\mathrm{Sample}\left(\mathrm{Uniform}\left(0,1\right),1\right)\left[1\right]$
 ${0.913375856139019}$ (4.1)



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)$

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

 >
 > $\mathrm{Array}\left(\mathrm{antisymmetric},1..2,1..2,\mathrm{random}\left(\right)\right)$

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)$

range=0..0.5:

 >
 > 

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)$

Number of elements in the array:

 > $\mathrm{numelems}\left(T\right)$
 ${336}$ (8.2)
 > $\mathrm{RandomArray}\left(1,2,\mathrm{distribution}=\mathrm{normal}\right)$
 >
 > $\mathrm{numelems}\left(A\right)$
 ${144}$ (8.3)



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)$

 > $\mathrm{RandomMatrix}\left(3,3,\mathrm{shape}=\mathrm{diagonal}\right)$

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)$

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)$



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)$

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)$
 > $\mathrm{RandomVector}\left[\mathrm{row}\right]\left(3,\mathrm{generator}=\mathrm{rand}\left(1..10\right)\right)$

Random vector with float entries:

 > $\mathrm{RandomVector}\left(3,\mathrm{generator}=0.1..0.9\right)$
 • 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)$

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.3)
 > $\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.4)



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)$



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

Return to the top of the page

Permutation

Recommended Command

Examples

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

combinat[randperm] - construct a random permutation.

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

Return a random permutation of n=5 positive integers:

 > $\mathrm{randperm}\left(5\right)$
 $\left[{3}{,}{4}{,}{5}{,}{1}{,}{2}\right]$ (15.1)

Random permutation from a list:

 > $\mathrm{randperm}\left(\left[5,7,9,10,12\right]\right)$
 $\left[{7}{,}{9}{,}{12}{,}{10}{,}{5}\right]$ (15.2)

Or, from a set:

 > $\mathrm{randperm}\left(\left\{a,b,c,d,e\right\}\right)$
 $\left[{a}{,}{c}{,}{b}{,}{d}{,}{e}\right]$ (15.3)

You can also generate something like the following:

 > $\mathrm{players}≔\mathrm{randperm}\left(\left[U,V,W,X,Y,Z\right]\right):$
 >
 ${\mathrm{teams}}{:=}\left[{V}{,}{Y}{,}{Z}\right]{,}\left[{X}{,}{U}{,}{W}\right]$ (15.4)





Return to the top of the page

Combination

Recommended Command

Examples

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

combinat[randcomb] - returns a random combination of elements.

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

Returns a random combination of five of the first 15 integers:

 > $\mathrm{randcomb}\left(15,5\right)$
 $\left\{{6}{,}{7}{,}{10}{,}{13}{,}{15}\right\}$ (16.1)

Random combination of three elements from a given list:

 > $\mathrm{randcomb}\left(\left[a,b,c,d,e\right],3\right)$
 $\left[{a}{,}{d}{,}{e}\right]$ (16.2)

Or, from a given set:

 > $\mathrm{randcomb}\left(\left\{4,5,6,7,8,9\right\},3\right)$
 $\left\{{7}{,}{8}{,}{9}\right\}$ (16.3)

You can also generate something like the following:

 >
 > $\mathrm{hand}≔\mathrm{randcomb}\left(\mathrm{cards},5\right)$
 ${\mathrm{hand}}{:=}\left[{\mathrm{Club3}}{,}{\mathrm{ClubKing}}{,}{\mathrm{Spade4}}{,}{\mathrm{Spade9}}{,}{\mathrm{SpadeQueen}}\right]$ (16.4)





Return to the top of the page

Random Partition

Recommended Command

Examples

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

combinat[randpart] - computes and returns a list of random partition of a positive integer n, where the sum of each element in the list equals n.

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

Return a list of random partition for n=5:

 > $\mathrm{randpart}\left(5\right)$
 $\left[{2}{,}{3}\right]$ (17.1)

You can check the list of all possible partitions for a given n=5 as follows:

 > $\mathrm{partition}\left(5\right)$
 $\left[\left[{1}{,}{1}{,}{1}{,}{1}{,}{1}\right]{,}\left[{1}{,}{1}{,}{1}{,}{2}\right]{,}\left[{1}{,}{2}{,}{2}\right]{,}\left[{1}{,}{1}{,}{3}\right]{,}\left[{2}{,}{3}\right]{,}\left[{1}{,}{4}\right]{,}\left[{5}\right]\right]$ (17.2)



Logic

Recommended Command

Examples

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

Logic[Random] - generates a random Boolean function. The default form is a disjunctive normal form (DNF).

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{Logic}\right):$
 >
 ${a}{}{b}{+}{b}{}{c}{+}{a}{+}{b}$ (18.1)

Use % to evaluate the last expression as follows:

 >
 ${1}$ (18.2)
 >
 ${a}{}{b}{}{c}{+}{a}{}{b}{+}{a}{}{c}$ (18.3)