# randn

Normally distributed random numbers

## Syntax

• `X = randn` example
• `X = randn(n)` example
• `X = randn(sz1,...,szN)` example
• `X = randn(sz)` example
• `X = randn(___,typename)` example
• `X = randn(___,'like',p)` example

## Description

example

````X = randn` returns a random scalar drawn from the standard normal distribution.```

example

````X = randn(n)` returns an `n`-by-`n` matrix of normally distributed random numbers.```

example

````X = randn(sz1,...,szN)` returns an `sz1`-by-...-by-`szN` array of random numbers where `sz1,...,szN` indicate the size of each dimension. For example, `randn(3,4)` returns a 3-by-4 matrix.```

example

````X = randn(sz)` returns an array of random numbers where size vector `sz` defines `size(X)`. For example, `randn([3 4])` returns a 3-by-4 matrix.```

example

``` `X = randn(___,typename)` returns an array of random numbers of data type `typename`. The `typename` input can be either `'single'` or `'double'`. You can use any of the input arguments in the previous syntaxes. ```

example

````X = randn(___,'like',p)` returns an array of random numbers like `p`; that is, of the same object type as `p`. You can specify either `typename` or `'like'`, but not both.```

The sequence of numbers produced by `randn` is determined by the internal settings of the uniform pseudorandom number generator that underlies `rand`, `randi`, and `randn`. You can control that shared random number generator using `rng`.

 Note:   Use the `rng` function instead of `rand` or `randn` with the `'seed'`, `'state'`, or `'twister'` inputs. For more information, see Replace Discouraged Syntaxes of rand and randn

## Examples

collapse all

### Matrix of Random Numbers

Generate a 5-by-5 matrix of normally distributed random numbers.

`r = randn(5)`
```r = 1.0347 0.8884 1.4384 -0.1022 -0.0301 0.7269 -1.1471 0.3252 -0.2414 -0.1649 -0.3034 -1.0689 -0.7549 0.3192 0.6277 0.2939 -0.8095 1.3703 0.3129 1.0933 -0.7873 -2.9443 -1.7115 -0.8649 1.1093```

### Bivariate Normal Random Numbers

Generate values from a bivariate normal distribution with specified mean vector and covariance matrix.

```mu = [1 2]; sigma = [1 0.5; 0.5 2]; R = chol(sigma); z = repmat(mu,10,1) + randn(10,2)*R```
```z = 1.3271 3.3688 2.0826 3.3279 2.0061 2.9663 0.3491 3.3285 1.2571 3.3585 0.0556 1.8450 -0.3218 0.4258 1.9248 1.6005 1.0000 3.5770 0.9451 -0.1597```

### Random Complex Numbers

Generate a single random complex number with normally distributed real and imaginary parts.

`a = randn + 1i*randn`
```a = 0.5377 + 1.8339i```

### Reset Random Number Generator

Save the current state of the random number generator and create a 1-by-5 vector of random numbers.

```s = rng; r = randn(1,5)```
```r = -0.0245 -1.9488 1.0205 0.8617 0.0012```

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 = randn(1,5)```
```r1 = -0.0245 -1.9488 1.0205 0.8617 0.0012```

Always use the `rng` function (rather than the `rand` or `randn` functions) to specify the settings of the random number generator. For more information, see Replace Discouraged Syntaxes of rand and randn.

### 3-D Array of Random Numbers

Create a 3-by-2-by-3 array of random numbers.

`X = randn([3,2,3])`
```X(:,:,1) = -0.0708 -2.1924 -2.4863 -2.3193 0.5812 0.0799 X(:,:,2) = -0.9485 0.8577 0.4115 -0.6912 0.6770 0.4494 X(:,:,3) = 0.1006 0.8979 0.8261 -0.1319 0.5362 -0.1472```

### Specify Data Type of Random Numbers

Create a 1-by-4 vector of random numbers whose elements are single precision.

```r = randn(1,4,'single') ```
```r = 0.5377 1.8339 -2.2588 0.8622 ```
`class(r)`
```ans = single```

### Clone Size from Existing Array

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

```A = [3 2; -2 1]; sz = size(A); X = randn(sz)```
```X = 1.0078 -0.5046 -2.1237 -1.2706```

It is a common pattern to combine the previous two lines of code into a single line:

`X = randn(size(A));`

### Clone Size and Data Type from Existing Array

Create a 2-by-2 matrix of single precision random numbers.

```p = single([3 2; -2 1]); ```

Create an array of random numbers that is the same size and data type as `p`.

```X = randn(size(p),'like',p) ```
```X = -0.3826 0.8257 0.6487 -1.0149 ```
`class(X)`
```ans = single```

### Clone Distributed Array

If you have Parallel Computing Toolbox™, create a 1000-by-1000 distributed array of random numbers with underlying data type `single`. For the `distributed` data type, the `'like'` syntax clones the underlying data type in addition to the primary data type.

`p = randn(1000,'single','distributed');`

Create an array of random numbers that is the same size, primary data type, and underlying data type as `p`.

`X = randn(size(p),'like',p);`
`class(X)`
```ans = distributed```
```classUnderlying(X) ```
```ans = single```

## Input Arguments

collapse all

### `n` — Size of square matrixinteger value

Size of square matrix, specified as an integer value.

• If `n` is `0`, then `X` is an empty matrix.

• If `n` is negative, then it is treated as `0`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### `sz1,...,szN` — Size of each dimension (as separate arguments)integer values

Size of each dimension, specified as separate arguments of integer values.

• If the size of any dimension is `0`, then `X` is an empty array.

• If the size of any dimension is negative, then it is treated as `0`.

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

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### `sz` — Size of each dimension (as a row vector)integer values

Size of each dimension, specified as a row vector of integer values. Each element of this vector indicates the size of the corresponding dimension:

• If the size of any dimension is `0`, then `X` is an empty array.

• If the size of any dimension is negative, then it is treated as `0`.

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

Example: `sz = [2,3,4]` creates a 2-by-3-by-4 array.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

### `typename` — Data type (class) to create`'double'` (default) | `'single'`

Data type (class) to create, specified as the string `'double'`, `'single'`, or the name of another class that provides `randn` support.

Example: `randn(5,'single')`

### `p` — Prototype of array to createnumeric array

Prototype of array to create, specified as a numeric array.

Example: `randn(5,'like',p)`

Data Types: `single` | `double`
Complex Number Support: Yes