Random numbers from discrete uniform distribution (2024)

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

nMaximum 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,...,szNSize 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 size sz1-by-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(n,3,1,1,1) produces a 3-by-1 vector of random numbers.

Example: 5,3,2

Data Types: single | double

szSize 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 size sz1-by-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(n,[3 1 1 1]) produces a 3-by-1 vector of random numbers.

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 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 user interface for random number generation.

Extended Capabilities

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.

Random numbers from discrete uniform distribution (1)

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)

Asia Pacific

Contact your local office

Random numbers from discrete uniform distribution (2024)

References

Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5804

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.