Random numbers from discrete uniform distribution
collapse all in page
Syntax
r = unidrnd(n)
r = unidrnd(n,sz1,...,szN)
r = unidrnd(n,sz)
Description
example
r = unidrnd(n)
generates random numbers from the discrete uniform distribution specified by its maximum value n
.
n
can be a scalar, vector, matrix, or multidimensional array.
example
r = unidrnd(n,sz1,...,szN)
generates an array of random numbers from the discrete uniform distribution with the scalar maximum value n
, where sz1,...,szN
indicates the size of each dimension.
example
r = unidrnd(n,sz)
generates an array of random numbers from the discrete uniform distribution with the scalar maximum value n
, where vector sz
specifies size(r)
.
Examples
collapse all
Array of Random Numbers from Several Discrete Uniform Distributions
Open Live Script
Generate an array of random numbers from the discrete uniform distributions. For each distribution, specify its maximum value.
Specify the maximum values of the distributions.
Generate random numbers from the discrete uniform distributions.
r = unidrnd(n)
r = 1×10 1 10 3 29 26 5 17 39 78 88
Array of Random Numbers from One Discrete Uniform Distribution
Open Live Script
Generate an array of random numbers from one discrete uniform distribution. Here, the maximum value n
is a scalar.
Use the unidrnd
function to generate random numbers from the discrete uniform distribution with the maximum value 100. The function returns one number.
R_scalar = unidrnd(100)
R_scalar = 82
Generate a 2-by-3 array of random numbers from the same distribution by specifying the required array dimensions.
R_array = unidrnd(100,2,3)
R_array = 2×3 91 92 10 13 64 28
Alternatively, specify the required array dimensions as a vector.
R_array = unidrnd(100,[2,3])
R_array = 2×3 55 97 98 96 16 96
Input Arguments
collapse all
n
— Maximum value
positive integer | array of positive integers
Maximum value, specified as a positive integer or array of positive integers.
Example: unidrnd(10)
Data Types: single
| double
sz1,...,szN
— Size of each dimension (as separate arguments)
integers
Size of each dimension, specified as separate arguments of integers. For example, specifying 5,3,2
generates a 5-by-3-by-2 array of random numbers from the discrete uniform distribution.
If n is an array, then the specified dimensions sz1,...,szN
must match the dimensions of n
.
If you specify a single value
sz1
, then r is a square matrix of sizesz1
-by-sz1
.If the size of any dimension is
0
or negative, thenr
is an empty array.Beyond the second dimension,
unidrnd
ignores trailing dimensions with a size of 1. For example,unidrnd
(
produces a 3-by-1 vector of random numbers.n
,3,1,1,1)
Example: 5,3,2
Data Types: single
| double
sz
— Size of each dimension (as a row vector)
row vector of integers
Size of each dimension, specified as a row vector of integers. For example, specifying [5 3 2]
generates a 5-by-3-by-2 array of random numbers from the discrete uniform distribution.
If n is an array, then the specified dimensions sz
must match the dimensions of n
.
If you specify a single value
[sz1]
, then r is a square matrix of sizesz1
-by-sz1
.If the size of any dimension is
0
or negative, thenr
is an empty array.Beyond the second dimension,
unidrnd
ignores trailing dimensions with a size of 1. For example,unidrnd
(
produces a 3-by-1 vector of random numbers.n
,[3 1 1 1])
Example: [5 3 2]
Data Types: single
| double
Output Arguments
collapse all
r
— Random numbers from discrete uniform distribution
scalar value | array of scalar values
Random numbers from the discrete uniform distribution, returned as a scalar value or an array of scalar values.
Data Types: single
| double
Alternative Functionality
unidrnd
is a function specific to discrete uniform distribution. Statistics and Machine Learning Toolbox™ also offers the generic function random, which supports various probability distributions. To userandom
, specify the probability distribution name and its parameters. Note that the distribution-specific functionunidrnd
is faster than the generic functionrandom
.To generate random numbers interactively, use randtool, a user interface for random number generation.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The generated code can return a different sequence of numbers than MATLAB® if either of the following is true:
The output is nonscalar.
An input parameter is invalid for the distribution.
For more information on code generation, see Introduction to Code Generation and General Code Generation Workflow.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
See Also
random | unidpdf | unidcdf | unidinv | unidstat
Topics
- Uniform Distribution (Discrete)
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office