Normal random numbers - MATLAB normrnd (2024)

Normal random numbers

collapse all in page

Syntax

r = normrnd(mu,sigma)

r = normrnd(mu,sigma,sz1,...,szN)

r = normrnd(mu,sigma,sz)

Description

example

r = normrnd(mu,sigma) generates a random number from the normal distribution with mean parameter mu and standard deviation parameter sigma.

r = normrnd(mu,sigma,sz1,...,szN) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension.

example

r = normrnd(mu,sigma,sz) generates an array of normal random numbers, where vector sz specifies size(r).

Examples

collapse all

Generate Normal Random Number

Open Live Script

Generate a single random value from the standard normal distribution.

rng('default') % For reproducibilityr = normrnd(0,1)
r = 0.5377

Reset Random Number Generator

Open Live Script

Save the current state of the random number generator. Then create a 1-by-5 vector of normal random numbers from the normal distribution with mean 3 and standard deviation 10.

s = rng;r = normrnd(3,10,[1,5])
r = 1×5 8.3767 21.3389 -19.5885 11.6217 6.1877

Restore the state of the random number generator to s, and then create a new 1-by-5 vector of random numbers. The values are the same as before.

rng(s);r1 = normrnd(3,10,[1,5])
r1 = 1×5 8.3767 21.3389 -19.5885 11.6217 6.1877

Clone Size from Existing Array

Open Live Script

Create a matrix of normally distributed random numbers with the same size as an existing array.

A = [3 2; -2 1];sz = size(A);R = normrnd(0,1,sz)
R = 2×2 0.5377 -2.2588 1.8339 0.8622

You can combine the previous two lines of code into a single line.

R = normrnd(1,0,size(A));

Input Arguments

collapse all

muMean
scalar value | array of scalar values

Mean of the normal distribution, specified as a scalar value or an array of scalar values.

To generate random numbers from multiple distributions, specify mu and sigma using arrays. If both mu and sigma are arrays, then the array sizes must be the same. If either mu or sigma is a scalar, then normrnd expands the scalar argument into a constant array of the same size as the other argument. Each element in r is the random number generated from the distribution specified by the corresponding elements in mu and sigma.

Example: [0 1 2; 0 1 2]

Data Types: single | double

sigmaStandard deviation
nonnegative scalar value | array of nonnegative scalar values

Standard deviation of the normal distribution, specified as a nonnegative scalar value or an array of nonnegative scalar values.

If sigma is zero, then the output r is always equal to mu.

To generate random numbers from multiple distributions, specify mu and sigma using arrays. If both mu and sigma are arrays, then the array sizes must be the same. If either mu or sigma is a scalar, then normrnd expands the scalar argument into a constant array of the same size as the other argument. Each element in r is the random number generated from the distribution specified by the corresponding elements in mu and sigma.

Example: [1 1 1; 2 2 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 probability distribution.

If either mu or sigma is an array, then the specified dimensions sz must match the common dimensions of mu and sigma after any necessary scalar expansion. The default values of sz are the common dimensions.

  • 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, normrnd ignores trailing dimensions with a size of 1. For example, specifying [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 — Normal random numbers
scalar value | array of scalar values

Normal random numbers, returned as a scalar value or an array of scalar values with the dimensions specified by sz1,...,szN or sz. Each element in r is the random number generated from the distribution specified by the corresponding elements in mu and sigma.

Alternative Functionality

  • normrnd is a function specific to normal distribution. Statistics and Machine Learning Toolbox™ also offers the generic function random, which supports various probability distributions. To use random, create a NormalDistribution probability distribution object and pass the object as an input argument or specify the probability distribution name and its parameters. Note that the distribution-specific function normrnd is faster than the generic function random.

  • Use randn to generate random numbers from the standard normal distribution.

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

References

[1] Marsaglia, G, and W. W. Tsang. “A Fast, Easily Implemented Method for Sampling from Decreasing or Symmetric Unimodal Density Functions.” SIAM Journal on Scientific and Statistical Computing. Vol. 5, Number 2, 1984, pp. 349–359.

[2] Evans, M., N. Hastings, and B. Peaco*ck. Statistical Distributions. 2nd ed. Hoboken, NJ: John Wiley & Sons, Inc., 1993.

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 | mvnrnd | lognrnd | NormalDistribution | randn | normcdf | normpdf

Topics

  • Random Number Generation
  • Normal Distribution

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.

Normal random numbers - MATLAB normrnd (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

Normal random numbers - MATLAB normrnd (2024)

References

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5834

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.