Work with Random Generators - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


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

Float

Rational

Random Data Sample

Polynomials

Array (Integer Entries)

Array (Float Entries)

Array (From a Distribution)

Matrix (Integer Entries)

Matrix (Float Entries)

Matrix (From a Given Distribution)

Vector

List

Set

Permutation

Combination

Random Partition

Logic

String

Graph

Related Topics

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 1012-1 and has a period of 2199371. 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.

 

 

restart

Returns a single random integer:

rand

959740139875

(1.1)

A procedure that can generate integers from a specified range:

Rrand100..200:

R

192

(1.2)

R

144

(1.3)

Returns a sequence of random numbers using:

seqR,i=1..5

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 1012.

• 

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 1012.

• 

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.

• 

rand

Return to the top of the page

Float

Recommended Command

Examples

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

RandomTools[MersenneTwister][GenerateFloat] -

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 2199371.

restart

withRandomToolsMersenneTwister:

GenerateFloat

0.0581869302

(2.1)

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

GenerateFloatdigits=5

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 2199371.

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

restart

withRandomTools:

Generaterational

104281139459499999999994

(3.1)

Generaterationalrange=14..34, character=open..closed, denominator=53000

779926500

(3.2)

Matrix3, 3, Generaterationaldenominator=24*identicalx+rationaldenominator=16, makeproc=true;

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:

positive, negative, nonnegative, nonpositive, and nonzero.

 

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.

restart

withStatistics:

SampleNormal0,1,3

SampleUniform0,1,11

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.

restart

randpolyx

7x5+22x455x394x2+87x56

(5.1)

Random homogeneous polynomial:

randpolyx,y

75x4y17x2y+80x244xy+71y282x

(5.2)

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

randpolyx,y, degree=4, terms=6, dense

23x4+87x3y+29x2y2+10xy3+95y4+44x3+98x2y61xy2+11y323x28xy49y229x47y+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.

restart

An Array of five integers from a given range:

Array1..5,random0..10

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

Array1..5, random,datatype=integer1

Arrayantisymmetric,1..2,1..2,random

 

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.

restart

Array1..5,frandom

range=0..0.5:

Array1..5, frandom0..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.

restart

withArrayTools:

RandomArray

0.2342493224

(8.1)

TRandomArray2,4,6,7

Number of elements in the array:

numelemsT

336

(8.2)

RandomArray1,2,distribution=normal

ARandomArray2,1,3,6, 4, distribution=normal

numelemsA

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 2199371.

restart

withLinearAlgebra:

Create an m×n random matrix:

RandomMatrix3,2

 

RandomMatrix3,3,shape=diagonal

 

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.

restart

withArrayTools:

Generate a 3-by-3 matrix of values:

RandomArray3

The above is equivalent to calling: RandomArray3,distribution=uniform.

 

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

RandomArray3,2,distribtion=normal

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

• 

ArrayTools[RandomArray]

• 

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.

restart

withStatistics:

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

MSampleNormal0,1,3,3

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 2199371.

 

restart

withLinearAlgebra:

Random vector with integer entries:

RandomVector3,generator=1..9

RandomVectorrow3,generator=rand1..10

Random vector with float entries:

RandomVector3,generator=0.1..0.9

• 

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 2199371.

 

restart

withRandomTools:

 

List of random integers:

Generatelistinteger,3

104281139460,306860183579,477575829529

(13.1)

 

List of random floats:

Generatelistfloat,2

8.34453987810-7,8.41242190510-10

(13.2)

 

List of Matrices with integer entries:

Generatelist'Matrix'integer,2,2,2

 

Use flav=listlist to generate a list of lists:

Generatelistlistrational,2

236602622351499999999994,170155408191499999999994,1052109720545454545454,347018765395499999999994

(13.3)

Generatelistlistintegerrange=1..10,3,2

7,7,3,3,5,3

(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 2199371.

restart

withRandomTools:

Set of random positive integers:

Generatesetposint,3

33467275626,57646025778,79394083865

(14.1)

Generate a set of random negative integers:

Generatesetnegintrange=10,3

8,4

(14.2)

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

 

Set of Vectors with nonzero rational entries:

Generateset'Vector'nonzerorange=5..5,2,2

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.

restart

withcombinat:

 

Return a random permutation of n=5 positive integers:

randperm5

3,4,5,1,2

(15.1)

Random permutation from a list:

randperm5,7,9,10,12

7,9,12,10,5

(15.2)

Or, from a set:

randperma,b,c,d,e

a,c,b,d,e

(15.3)

You can also generate something like the following:

playersrandpermU,V,W,X,Y,Z:

teamsplayers1..3, players4..

teams:=V,Y,Z,X,U,W

(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.

restart

withcombinat:

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

randcomb15,5

6,7,10,13,15

(16.1)

Random combination of three elements from a given list:

randcomba,b,c,d,e,3

a,d,e

(16.2)

Or, from a given set:

randcomb4,5,6,7,8,9,3

7,8,9

(16.3)

You can also generate something like the following:

cardsseqopcats,1..10, cats,Jack,cats,Queen,cats,King, cats,Ace,s in Heart,Club,Spade,Diamond:

handrandcombcards,5

hand:=Club3,ClubKing,Spade4,Spade9,SpadeQueen

(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.

restart

withcombinat:

Return a list of random partition for n=5:

randpart5

2,3

(17.1)

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

partition5

1,1,1,1,1,1,1,1,2,1,2,2,1,1,3,2,3,1,4,5

(17.2)

Return to the top of the page

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

restart

withLogic:

Randoma,b,c, form=MOD2

ab+bc+a+b

(18.1)

Use % to evaluate the last expression as follows:

eval%,a=1,b=0,c=1 mod 2

1

(18.2)

Randoma,b,c, form=MOD2

abc+ab+ac

(18.3)

Randoma,b

a &and b &or &nota &and &notb

(18.4)

Random Boolean with conjunctive normal form:

Randoma,b,form=CNF

a &or b &and &nota &or &notb

(18.5)

Return to the top of the page

String

Recommended Command

Examples

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

StringTools[Random] - generates a random string.

restart

withStringTools:

Return a string of length len=20:

Random20, 'alpha'

mxYYTxRAPYLRkyzmiKLE

(19.1)

where characters are chosen from the letters A-Z or a-z with equal probability.

 

The following call restricts the characters returned to uppercase letters (alphabet=upper):

Random20, 'upper'

KNGKYYTRAPYLRKLELFAM

(19.2)

 

Or, returns 20 random lowercase letters:

Random20, 'lower'

glqpxdsfarkdmjgrtqiw

(19.3)

 

Generate a random word:

wordsEssayTools:-GetWordList:

Rrand1..numelemswords:

wordsR

spark

(19.4)

For a list of character class names recognized by alphabet, see StringTools[Random].

 

For more information on generating random words, see EssayTools[GetWordList].

Return to the top of the page

Graph

Recommended Command

Examples

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

GraphTheory[RandomGraphs][RandomGraph] - uses the calls RandomGraph(n,p) and RandomGraph(n,m) to create undirected, unweighted graphs. Options such as directed and weights can be added to these calls to generate directed and weighted graphs, respectively.

restart

withGraphTheory:

withGraphTheoryRandomGraphs:

Create an undirected, unweighted graph with n=5 vertices and probability p=0.5:

GRandomGraph5,0.5

G:=Graph 4: an undirected unweighted graph with 5 vertices and 7 edge(s)

(20.1)

DrawGraphG

Alternatively, carry out the one-step execution:

DrawGraphRandomGraph5,0.5

A random, unweighted graph with n=8 vertices and m=10 edges:

DrawGraphRandomGraph8,10

The following specification can only return one form:

DrawGraphRandomGraph5,10

Use RandomGraph(n, m, option=directed) to create a random, directed graph:

DrawGraphRandomGraph8,10,directed

For other routines that generate random graphs with particular properties or structures, see the GraphTheory[RandomGraphs] subpackage.

Return to the top of the page

Related Topics

The How Do I... topics cover the essentials for doing mathematics in Maple. Learn more about available tools and features, such as palettes and the context panel.

How Do I...

...Enter a ComplexNumber?

...Enter a Function?

...Enter a Matrix?

...Enter a Simple Expression?

...Evaluate an Expression?

...Import Tabular Data?

...Plot a Function?

...Plot a Straight Line?

...Plot Multiple Functions?

...Solve an Ordinary Differential Equation?

 

Tools and Features

Palettes

Context Panel

Command Completion

Equation Labels

Assistants

Maple Help

Plotting Guide

Applications

Example Worksheets

Manuals

Refer to Help>Quick Reference for basic Getting Started tips.

See Also

MaplePortal

dsolve

ArrayTools

RandomTools

LinearAlgebra

StringTools

Statistics