# normrnd

Normal random numbers

## Syntax

``r = normrnd(mu,sigma)``
``r = normrnd(mu,sigma,sz1,...,szN)``
``r = normrnd(mu,sigma,[sz1,...,szN])``

## Description

example

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

example

````r = normrnd(mu,sigma,sz1,...,szN)` or `r = normrnd(mu,sigma,[sz1,...,szN])` generates a `sz1`-by-⋯-by-`szN` array of normal random numbers.```

## Examples

collapse all

Generate a single random value from the standard normal distribution.

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

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 ```

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

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`

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`

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 probability distribution.

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

• 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, `normrnd` ignores trailing dimensions with a size of 1. For example, `normrnd``(3,1,1,1)` produces a 3-by-1 vector of random numbers.

Data Types: `single` | `double`

## Output Arguments

collapse all

Normal random numbers, returned as a scalar value or an array of scalar values with the dimensions specified by `sz1,...,szN`. 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 random number generation user interface.

 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.

 Evans, M., N. Hastings, and B. Peacock. Statistical Distributions. 2nd ed. Hoboken, NJ: John Wiley & Sons, Inc., 1993.