Skip to main content
Engineering LibreTexts

9.1: MATLAB Random Number Functions

  • Page ID
    83372
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    By Carey A. Smith

    Random numbers are useful in MATLAB simulations of real-world signals and events which have uncertainty. There are 3 main MATLAB random number generators:

    randi(IMAX,M,N) Random Integers

    RndInt = randi(IMAX,M,N) returns an M-by-N matrix of uniformly distributed pseudo-random integers from 1 to IMAX.

    Example 1A: Generate a vector of 8 dice rolls:

    Dice = randi(6,1,8)

    Example 1B: Generate a 4x3 array of random integers between 0 and 100

    RndInt_4_3 = randi(100,4,3)

    rand(M,N) Random Decimals

    RndDec = rand(M,N) returns an M-by-N matrix of uniformly distributed pseudo-random decimal numbers between 0 and 1.

    Example 2A: Generate 8 random decimals between 0 and 1:

    RndDec2A = rand(1,8)

    The vector can be scaled to be between any 2 numbers by adding an offset number and multiplying by a scale factor.

    Example 2B: Generate 8 random decimals between –1 and 1:

    RndDec2B = 2*rand(1,8) - 1

    randn(M,N) Normally Distributed Random Numbers

    RndNorm = randn(M,N) returns an M-by-N matrix of pseudo-random values drawn from the standard normal distribution, which has a mean (average) = 0 and a standard deviation = 1.

    The standard deviation is a measure of the spread of the data. For a further explanation, watch this video:

    “Standard Deviation - Explained and Visualized”, by Jeremy Blitz-Jones

    https://www.youtube.com/watch?v=MRqtXL2WX2M

    The Normal Distribution is also called the “Gaussian Distribution”.

    Example 3A:

    RndNorm3A = randn(1,5)

    The result can be scaled to have any mean and standard deviation by adding an offset number and multiply by a scale factor.

    Example \(\PageIndex{1}\) Pulse Rates

    % Example of randn() for pulse rates
    % Generate a sample of 30 random pulse rates 
    % with a nominal mean = 90 
    % and a nominal standard deviation = 15.
    n = 30
    pmean    = 90
    pstd_dev = 15
    pulse = pmean + pstd_dev*randn(1,n); % This creates a 1xn vector

    % Compute the sample mean and standard deviation.
    pulse_mean = mean(pulse)
    pulse_std = std(pulse)

    %% Create a histogram of the results.
    % For MATLAB use the histogram() function
    % For Octave, use the older hist() function

    %% MATALB Default histogram
    figure;
    histogram(pulse)
    title('MATLAB Default bins')

    %% Octave Default histogram

    graphics_toolkit("fltk") % Needed for Octave 7.2
    figure;
    hist(pulse)
    title('Octave Default hist')

    %% MATLAB Custom histogram 
    edges = 60 : 10 : 120;
    figure;
    histogram(pulse, edges)
    title('MATLAB Custom bin edges histogram')

    Solution

    MATLAB Default

    MATLAB_Default_histogram.png

    Octave:

    pulse_Octave_hist.png

    MATLAB Custom bin edges

    MATLAB_Custom bin edges.png

    .

     

    Example 3C: A radar signal has magnitude 20 with noise of magnitude = 4 (standard deviation)

    Received_signal = 20 + 4*randn(1,5)

    Standard_Normal_Distribution.jpg

    Figure \(\PageIndex{1}\): Standard Normal Probability Distribution

    The total area under the curve = 1.00. Approximately:

    68% is between -1 and +1 standard deviations

    95% is between -2 and +2 standard deviations

    99.7% is between -3 and +3 standard deviations

    The normal distribution approximates many things, such as:

    • Noise in signals
    • Measurement errors
    • Clinical trials of medicines and vaccines
    • Probability and statistics
    • Biological data, such as weights, heights, pulse rates, blood pressure, diabetes, etc.

    The “Central Limit Theorem” states that the sum or average of random variables becomes normal as the number of data (the sample size) increases, even if the raw data is not normally distributed.

    You may want to watch Mike’s Galton Machine video. It uses ball bearings to build up a normal distribution: https://www.youtube.com/watch?v=VQepRP-f6GA

    See also: https://en.Wikipedia.org/wiki/Galton_board

     

    The Standard Normal Probability Distribution plot can be created with this code:

    % NormalDistribution.m

    x = -4:0.01:4;

    mu = 0; % mean

    std_dev = 1; % standard deviation

    y = (1/(std_dev*sqrt(2*pi))) * exp((-1/2)*((x-mu)/std_dev).^2);

    figure;

    plot(x,y,'LineWidth',4)

    grid on;

    title('Standard Normal Distribution','FontSize',14)

    xlabel('Number of Standard Deviations','FontSize',12)

    ax = gca

    ax.LineWidth = 2


    This page titled 9.1: MATLAB Random Number Functions is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.