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.
Random Data Sample
Array (Integer Entries)
Array (Float Entries)
Array (From a Distribution)
Matrix (Integer Entries)
Matrix (Float Entries)
Matrix (From a Given Distribution)
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.
Returns a single random integer:
A procedure that can generate integers from a specified range:
Returns a sequence of random numbers using:
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
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.
Uses the digits=integer option to specify how many random digits should be returned:
Other available commands to generate random floats are:
RandomTools[MersenneTwister][GenerateFloat64] - uses the MersenneTwister algorithm to generate a pseudo-random float 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].
Generaterationalrange=14..34, character=open..closed, denominator=53000
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.
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.
Random homogeneous polynomial:
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
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:
Create a random Array of n-byte hardware integer, where n is one of 1, 2, 4, or 8:
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.
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.
Number of elements in the array:
A≔RandomArray2,1,3,6, 4, distribution=normal
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.
Create an m×n random matrix:
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:
The above is equivalent to calling: RandomArray3,distribution=uniform.
Generate a 3-by-2 Matrix using normal distribution:
Other available commands to construct a Matrix with random floats are:
RandomTools[Generate] - by combining flavors Matrix and distribution.
Generate a 3-by-3 Matrix using Normal distribution:
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:
Random vector with float entries:
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:
List of random floats:
List of Matrices with integer entries:
Use flav=listlist to generate a list of lists:
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:
Generate a set of random negative integers:
where, n=3 is the maximum number of entries in the set.
Set of Vectors with nonzero rational entries:
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.
Return a random permutation of n=5 positive integers:
Random permutation from a list:
Or, from a set:
You can also generate something like the following:
combinat[randcomb] - returns a random combination of elements.
Returns a random combination of five of the first 15 integers:
Random combination of three elements from a given list:
Or, from a given set:
cards≔seqopcats,1..10, cats,Jack,cats,Queen,cats,King, cats,Ace,s in Heart,Club,Spade,Diamond:
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:
You can check the list of all possible partitions for a given n=5 as follows:
Logic[Random] - generates a random Boolean function. The default form is a disjunctive normal form (DNF).
Use % to evaluate the last expression as follows:
eval%,a=1,b=0,c=1 mod 2
a &and b &or ¬⁡a &and ¬⁡b
Random Boolean with conjunctive normal form:
a &or b &and ¬⁡a &or ¬⁡b
StringTools[Random] - generates a random string.
Return a string of length len=20:
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):
Or, returns 20 random lowercase letters:
Generate a random word:
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.
Create an undirected, unweighted graph with n=5 vertices and probability p=0.5:
G:=Graph 4: an undirected unweighted graph with 5 vertices and 7 edge(s)
Alternatively, carry out the one-step execution:
A random, unweighted graph with n=8 vertices and m=10 edges:
The following specification can only return one form:
Use RandomGraph(n, m, option=directed) to create a random, directed graph:
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
Refer to Help>Quick Reference for basic Getting Started tips.
Download Help Document