Engee documentation

randn

Normally distributed random numbers.

Library

EngeeDSP

Syntax

Function call

  • X = randn() — returns a random scalar value taken from a standard normal distribution.

  • X = randn(n) — returns a matrix of size n×n normally distributed random numbers.

  • X = randn(sz1,…,szN) — returns an array of random numbers of the size sz1 × … × szN, where sz1,…,szN denote the size of each dimension. For example, randn(3,4) returns a matrix of size 3×4.

  • X = randn(sz) — returns an array of random numbers, where is a vector of size sz defines size(X). For example, randn([3, 4]) returns a matrix of size 3×4.

  • X = randn(___,typename) — returns an array of random numbers of the type typename. Input parameter typename maybe "Float32" or "Float64". You can use any combination of the input arguments presented in the previous examples.

  • X = randn(___,like=p) — returns an array of random numbers similar to p, that is, having the same data type and number type (real or complex) as p. You can specify either typename, or like but not both at the same time.

  • X = randn(s,___) — generates numbers from a stream of random numbers s instead of the default global flow. To create a stream, use RandStream. You can specify s, and then any of the combinations of input arguments presented in the previous examples.

Arguments

Input arguments

# n is the size of the square matrix

+ integer value

Details

The size of the square matrix, set as an integer.

If n equally 0 Then X — an empty matrix.

Типы данных

Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64

# sz1,…​,szN — the size of each dimension (as separate arguments)

+ integer values

Details

The size of each dimension, set as separate arguments to integer values.

  • If the size of any dimension is 0 Then X — an empty array.

  • Beyond the second dimension, the function randn ignores all subsequent measurements with size 1. For example, randn(3,1,1) creates a vector of random numbers of the size 3×1.

Типы данных

Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64

# sz — the size of each dimension (in the form of a row vector)

+ integer values

Details

The size of each dimension, specified as a vector, is a string of integer values.

  • If the size of any dimension is 0 Then X — an empty array.

  • Beyond the second dimension randn ignores all subsequent measurements with size 1. For example, randn([3 1 1]) creates a vector of random numbers of the size 3×1.

Типы данных

Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64

# typename — the data type (class) to be created

+ "Float64" (default) | "Float32"

Details

The data type (class) to be created can be specified as "Float64", "Float32" or as the name of another class that supports randn.

# s — random number stream

+ the RandStream object

Details

A stream of random numbers specified as an object RandStream.

# p is the prototype of the array being created

+ numeric array

Details

The prototype of the array being created, specified as a numeric array.

Типы данных

Float32, Float64

Support for complex numbers

Yes

Output arguments

# X is the output array

+ scalar | vector | the matrix | multidimensional array

Details

The output array returned as a scalar, vector, matrix, or multidimensional array.

Examples

The matrix of random numbers

Details

Generate the matrix 5×5 normally distributed random numbers.

import EngeeDSP.Functions: randn

r = randn(5)
5×5 Matrix{Float64}:
  2.97447   -0.106555   2.02373    0.296171   0.733738
 -0.622585  -0.215161   0.777789   1.20078    0.120332
  1.9203     0.47349   -0.548902   1.09017    1.13633
  0.961149   1.36564   -0.126011  -0.358703  -0.686773
 -0.557803  -1.6378     0.29958   -0.129928   0.471683

Two-dimensional normal random number distribution

Details

We will generate values based on a two-dimensional normal distribution with a given expectation vector and a covariance matrix.

import EngeeDSP.Functions: randn
using LinearAlgebra

mu = [1 2]
sigma = [1 0.5; 0.5 2]
R = cholesky(sigma).U
z = repeat(mu, 10, 1) + randn(10, 2) * R
10×2 Matrix{Float64}:
  1.6715     3.51099
 -0.207487  -0.121175
  1.71724    0.944637
  2.63024    1.74425
  1.48889   -1.65047
  2.03469    4.42014
  1.72689    2.79363
  0.696559   0.849603
  1.29387    3.95967
  0.212717  -0.657765

A three-dimensional array of random numbers

Details

Create an array of random numbers of the size 3×2×3.

import EngeeDSP.Functions: randn

X = randn([3,2,3])
3×2×3 Array{Float64, 3}:
[:, :, 1] =
 -0.102242   0.312859
 -0.241447  -0.86488
  0.319207  -0.0300513

[:, :, 2] =
 -0.164879   1.10927
  0.627707  -0.863653
  1.09327    0.0773591

[:, :, 3] =
 -1.21412      1.53263
 -1.1135      -0.769666
 -0.00684933   0.371379

Specifying the data type of random numbers

Details

Create a vector of random numbers of the size 1×4, the elements of which have single precision.

import EngeeDSP.Functions: randn

r = randn(1,4,"Float32")
1×4 Matrix{Float32}:
 -0.225584  1.11736  -1.08906  0.0325575
typeof(r)
Matrix{Float32}

The size is determined by the existing array

Details

Let’s create a matrix of normally distributed random numbers of the same size as the existing array.

import EngeeDSP.Functions: randn

A = [3 2; -2 1]
sz = size(A)
X = randn(sz...)
2×2 Matrix{Float64}:
 -0.615602  -0.192419
  0.748077   0.88861

It is a common practice to combine the two previous lines of code into one.

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

The size and type of data are determined by the existing array

Details

Create a matrix 2×2 single-precision random numbers.

p = Float32[3 2; -2 1];

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

X = randn(size(p)...,like=p)
2×2 Matrix{Float32}:
 0.197811  -0.804466
 1.5877     0.696624
typeof(X)
Matrix{Float32}

Additional Info

Standard real and standard complex normal distributions

Details

When generating random real numbers, the function randn generates data that corresponds to a standard normal distribution.:

Here — a random real value with a mathematical expectation and the variance .

When generating random complex numbers, for example, when using the command randn(…​,like=1im), function randn generates data that corresponds to the standard complex normal distribution.:

Here is a random complex quantity, the real and imaginary parts of which are independent normally distributed random variables with mathematical expectation and the variance .

Pseudorandom Number Generator

Details

At the heart of the number generator randn There is a pseudorandom generator that creates a deterministic sequence of numbers that seems random. These numbers are predictable if the initial number and the deterministic algorithm of the generator are known. Although the generated numbers are not truly random, they pass various statistical tests for randomness, satisfying the condition of independence and the same distribution, justifying the name "pseudo-random."

Recommendations

The sequence of numbers generated by the function randn, is determined by the internal settings of the universal the pseudorandom number generator underlying the functions rand, randi and randn.