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 to and has a period of . 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:
>
|
|
| (1.4) |
|
Other available commands to generate random integers:
|
|
|
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 .
|
>
|
|
Uses the digits=integer option to specify how many random digits should be returned:
>
|
|
|
Other available commands to generate random floats are:
|
|
|
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 .
For a list of Maple types, flavor templates, and data structures that are recognized by Generate, see RandomTools[Generate].
|
| (3.1) |
>
|
|
>
|
|
|
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.
|
>
|
|
>
|
|
|
Other available functions to generate random data sample (float):
|
|
|
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 .
|
| (5.1) |
Random homogeneous polynomial:
| (5.2) |
Use various options, such as coeffs, expons, terms, degree, dense, and homogeneous in order to return appropriate outputs. For example:
>
|
|
| (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.
|
|
|
|
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.
|
An Array of five integers from a given range:
>
|
|
Create a random Array of -byte hardware integer, where is one of , , , or :
>
|
|
>
|
|
|
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 .
|
>
|
|
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.
|
>
|
|
Number of elements in the array:
>
|
|
>
|
|
|
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.
|
|
|
|
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 random matrix where all the entries are integers in the range of and has a period of .
|
>
|
|
Create an 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:
•
|
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 matrix where entries are random floats.
|
Generate a 3-by-3 matrix of values:
The above is equivalent to calling: .
Generate a 3-by-2 Matrix using normal distribution:
>
|
|
|
Other available commands to construct a Matrix with random floats are:
|
|
|
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.
|
Generate a 3-by-3 Matrix using Normal distribution:
>
|
|
|
Other available commands to construct a Matrix with random floats are:
|
|
|
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 and has a period of .
|
>
|
|
Random vector with integer entries:
>
|
|
>
|
|
Random vector with float entries:
>
|
|
|
•
|
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 .
|
List of random integers:
>
|
|
| (13.1) |
List of random floats:
>
|
|
| (13.2) |
List of Matrices with integer entries:
>
|
|
Use flav=listlist to generate a list of lists:
>
|
|
| (13.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 .
|
Set of random positive integers:
>
|
|
| (14.1) |
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].
|
|
|
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.
|
Return a random permutation of n=5 positive integers:
| (15.1) |
Random permutation from a list:
>
|
|
| (15.2) |
Or, from a set:
>
|
|
| (15.3) |
You can also generate something like the following:
>
|
|
>
|
|
| (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.
|
Returns a random combination of five of the first 15 integers:
| (16.1) |
Random combination of three elements from a given list:
>
|
|
Or, from a given set:
>
|
|
You can also generate something like the following:
>
|
|
>
|
|
| (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.
|
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:
| (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).
|
>
|
|
| (18.1) |
Use % to evaluate the last expression as follows:
>
|
|
>
|
|
| (18.3) |
| (18.4) |
Random Boolean with conjunctive normal form:
>
|
|
| (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.
|
Return a string of length len=20:
>
|
|
| (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):
>
|
|
| (19.2) |
Or, returns 20 random lowercase letters:
>
|
|
| (19.3) |
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].
|
|
|
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.
|
>
|
|
Create an undirected, unweighted graph with n=5 vertices and probability p=0.5:
>
|
|
| (20.1) |
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.
|
|
|
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.
Refer to Help>Quick Reference for basic Getting Started tips.
|
|
|
|
|
|