unidrnd

Random numbers from discrete uniform distribution

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)or r = unidrnd(n,[sz1,...,szN]) generates an sz1-by-⋯-by-szN array containing random numbers from the discrete uniform distribution with the scalar maximum value n.

Examples

collapse all

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.

n = 1:10:100;

Generate random numbers from the discrete uniform distributions.

r = unidrnd(n)
r = 1×10

     1    10     3    29    26     5    17    39    78    88

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

Maximum value, specified as a positive integer or array of positive integers.

Example: unidrnd(10)

Data Types: single | double

Size of each dimension, specified as integers or a row vector of integers. For example, specifying 5,3,2 or [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 size sz1.

  • If the size of any dimension is 0 or negative, then r is an empty array.

  • Beyond the second dimension, unidrnd ignores trailing dimensions with a size of 1. For example, unidrnd(3,1,1,1) produces a 3-by-1 vector of random numbers.

Example: 5,3,2 or [5,3,2]

Data Types: single | double

Output Arguments

collapse all

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 use random, specify the probability distribution name and its parameters. Note that the distribution-specific function unidrnd is faster than the generic function random.

  • To generate random numbers interactively, use randtool, a random number generation user interface.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Introduced before R2006a