How Do I - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Getting Started : How Do I... : HowDoI/WorkWithRandomGenerators

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;

13x151634x3161112x91616x91678x182324x11612x+31623x+5878x+116

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

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

1.215249966958030.9163112505048080.760539518007130

(4.1)

SampleUniform0,1,11

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.

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

43695

(6.1)

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

Array1..5, random,datatype=integer1

821224911536

(6.2)

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

051990904054251702519909040542517020

(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 integer entries. The default range for frandom is 0..1.

restart

Array1..5,frandom

0.2400759523804695260.9901099573001381770.9489250879788927450.1998728702427862160.140143840101215611

(7.1)

range=0..0.5:

Array1..5, frandom0..0.5

0.2700690005021033070.2383190363933976320.3898446842545446040.1943075866344387560.00826029311923809351

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

restart

withArrayTools:

RandomArray

0.2342493224

(8.1)

TRandomArray2,4,6,7

T:= 1..2 x 1..4 x 1..6 x 1..7 ArrayData Type: float8Storage: rectangularOrder: Fortran_order

(8.2)

Number of elements in the array:

numelemsT

336

(8.3)

RandomArray1,2,distribution=normal

0.4925683770276580.0395071892741721

(8.4)

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

A:= 1..2 x 1..1 x 1..3 x 1..6 x 1..4 ArrayData Type: float8Storage: rectangularOrder: Fortran_order

(8.5)

numelemsA

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

restart

withLinearAlgebra:

Create an m×n random matrix:

RandomMatrix3,2

94537211225

(9.1)

 

RandomMatrix3,3,shape=diagonal

20007200076

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

restart

withArrayTools:

Generate a 3-by-3 matrix of values:

RandomArray3

0.9575068354342980.09754040499940950.1269868162935060.5468815192049840.6323592462254100.9057919370756190.2784982188670480.9133758561390190.814723686393179

(10.1)

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

 

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

RandomArray3,2,distribtion=normal

0.09754040499940950.1269868162935060.6323592462254100.9057919370756190.9133758561390190.814723686393179

(10.2)

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

M:=1.072424127998270.2144667452452911.634856754343900.3290778705470650.02542812804238461.571172171754300.6170919369097901.728821284177830.170358421410408

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

 

restart

withLinearAlgebra:

Random vector with integer entries:

RandomVector3,generator=1..9

171

(12.1)

RandomVectorrow3,generator=rand1..10

1062

(12.2)

Random vector with float entries:

RandomVector3,generator=0.1..0.9

0.6945059744999328990.7061921044626667810.642988123886218732

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

348256258834181644806914482438952754383395241439,205804620411390317514418461153517356245484114740

(13.3)

 

Use flav=listlist to generate a list of lists:

Generatelistlistrational,2

236602622351499999999994,170155408191499999999994,1052109720545454545454,347018765395499999999994

(13.4)

Generatelistlistintegerrange=1..10,3,2

7,7,3,3,5,3

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

55244811165227272727271213584031357249999999997,7734420225934999999999941160154170167499999999994

(14.3)

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:

randpe