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
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 219937−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.
restart
Returns a single random integer:
rand
959740139875
A procedure that can generate integers from a specified range:
R≔rand100..200:
R
192
144
Returns a sequence of random numbers using:
seqR,i=1..5
144,195,105,197,158
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.
Return to the top of the page
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 219937−1.
withRandomToolsMersenneTwister:
GenerateFloat
0.0581869302
Uses the digits=integer option to specify how many random digits should be returned:
GenerateFloatdigits=5
0.13612
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.
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 219937−1.
For a list of Maple types, flavor templates, and data structures that are recognized by Generate, see RandomTools[Generate].
withRandomTools:
Generaterational
−104281139459499999999994
Generaterationalrange=14..34, character=open..closed, denominator=53000
779926500
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.
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.
withStatistics:
SampleNormal0,1,3
SampleUniform0,1,11
0.913375856139019
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.
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.
randpolyx
−7⁢x5+22⁢x4−55⁢x3−94⁢x2+87⁢x−56
Random homogeneous polynomial:
randpolyx,y
−75⁢x4⁢y−17⁢x2⁢y+80⁢x2−44⁢x⁢y+71⁢y2−82⁢x
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
−23⁢x4+87⁢x3⁢y+29⁢x2⁢y2+10⁢x⁢y3+95⁢y4+44⁢x3+98⁢x2⁢y−61⁢x⁢y2+11⁢y3−23⁢x2−8⁢x⁢y−49⁢y2−29⁢x−47⁢y+40
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.
Array - use the optional parameter random to generate Arrays with random integer entries. The default range for random is all possible integers.
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.
Array - use the optional parameter frandom to generate Arrays with random floating-point entries. The default range for frandom is 0..1.
Array1..5,frandom
range=0..0.5:
Array1..5, frandom0..0.5
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.
withArrayTools:
RandomArray
0.2342493224
T≔RandomArray2,4,6,7
Number of elements in the array:
numelemsT
336
RandomArray1,2,distribution=normal
A≔RandomArray2,1,3,6, 4, distribution=normal
numelemsA
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].
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 219937−1.
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.
ArrayTools[RandomArray] - calls to return an m×n matrix where entries are random floats.
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.
Generate a 3-by-3 Matrix using Normal distribution:
M≔SampleNormal0,1,3,3
RandomTools[Generate] - by combining flavors Matrix and distribution. (CPU time=5.16 ms)
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 219937−1.
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.
RandomTools[Generate(list)] - uses the flavor list for the Generate command to return a list of random objects, which are specified by other flavors.
List of random integers:
Generatelistinteger,3
−104281139460,−306860183579,−477575829529
List of random floats:
Generatelistfloat,2
8.344539878⁢10-7,8.412421905⁢10-10
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
Generatelistlistintegerrange=1..10,3,2
7,7,3,3,5,3
For a complete list of Maple types, flavors, templates, and data structures that can be combined with flav=list, see RandomTools[Generate].
RandomTools[Generate(set)] - uses the flavor set to generate a set of random objects.
Set of random positive integers:
Generatesetposint,3
33467275626,57646025778,79394083865
Generate a set of random negative integers:
Generatesetnegintrange=−10,3
−8,−4
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].
combinat[randperm] - construct a random permutation.
withcombinat:
Return a random permutation of n=5 positive integers:
randperm5
3,4,5,1,2
Random permutation from a list:
randperm5,7,9,10,12
7,9,12,10,5
Or, from a set:
randperma,b,c,d,e
a,c,b,d,e
You can also generate something like the following:
players≔randpermU,V,W,X,Y,Z:
teams≔players1..3, players4..
teams:=V,Y,Z,X,U,W
combinat[randcomb] - returns a random combination of elements.
Returns a random combination of five of the first 15 integers:
randcomb15,5
6,7,10,13,15
Random combination of three elements from a given list:
randcomba,b,c,d,e,3
a,d,e
Or, from a given set:
randcomb4,5,6,7,8,9,3
7,8,9
cards≔seqopcats,1..10, cats,Jack,cats,Queen,cats,King, cats,Ace,s in Heart,Club,Spade,Diamond:
hand≔randcombcards,5
hand:=Club3,ClubKing,Spade4,Spade9,SpadeQueen
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.
Return a list of random partition for n=5:
randpart5
2,3
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
Logic[Random] - generates a random Boolean function. The default form is a disjunctive normal form (DNF).
withLogic:
Randoma,b,c, form=MOD2
a⁢b+b⁢c+a+b
Use % to evaluate the last expression as follows:
eval%,a=1,b=0,c=1 mod 2
1
a⁢b⁢c+a⁢b+a⁢c
Randoma,b
a &and b &or ¬⁡a &and ¬⁡b
Random Boolean with conjunctive normal form:
Randoma,b,form=CNF
a &or b &and ¬⁡a &or ¬⁡b
StringTools[Random] - generates a random string.
withStringTools:
Return a string of length len=20:
Random20, 'alpha'
mxYYTxRAPYLRkyzmiKLE
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
Or, returns 20 random lowercase letters:
Random20, 'lower'
glqpxdsfarkdmjgrtqiw
Generate a random word:
words≔EssayTools:-GetWordList:
R≔rand1..numelemswords:
wordsR
spark
For a list of character class names recognized by alphabet, see StringTools[Random].
For more information on generating random words, see EssayTools[GetWordList].
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.
withGraphTheory:
withGraphTheoryRandomGraphs:
Create an undirected, unweighted graph with n=5 vertices and probability p=0.5:
G≔RandomGraph5,0.5
G:=Graph 4: an undirected unweighted graph with 5 vertices and 7 edge(s)
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.
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
Download Help Document