0% found this document useful (0 votes)
34 views

Section 7: Random Number Generators (Matlab Examples) .: Main Page Eeng School Dcu

This document discusses random number generators in Matlab. It describes built-in Matlab functions for generating random numbers from uniform and normal distributions, including RAND, RANDN, SPRAND, and SPRANDN. It also provides an example of a linear congruential generator function (LCG) that generates random integers based on seed values and allows calculating the period and other statistics. The document explains that while the LCG function demonstrates how to generate random numbers, Matlab's predefined functions are preferable for most uses.

Uploaded by

AshRofin Moch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Section 7: Random Number Generators (Matlab Examples) .: Main Page Eeng School Dcu

This document discusses random number generators in Matlab. It describes built-in Matlab functions for generating random numbers from uniform and normal distributions, including RAND, RANDN, SPRAND, and SPRANDN. It also provides an example of a linear congruential generator function (LCG) that generates random integers based on seed values and allows calculating the period and other statistics. The document explains that while the LCG function demonstrates how to generate random numbers, Matlab's predefined functions are preferable for most uses.

Uploaded by

AshRofin Moch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Main Page EEng School DCU

Section 7: Random Number Generators (Matlab


Examples).
Monte Carlo Methods
Random Numbers
Random Number Cycle
Linear Congruential Generators
Multiplicative LCG
Mixed LCG
lcg.m this Matlab code implements a comprehensive function LCG(a, c, m, X0)
with the following inputs and outputs:

inputs:
m is the modulus
a the multiplier
c the increment
X0 the initial seed
outputs:
P period of the sequence
mean average of one period of the real random numbers
var variance of the sequence of one period of the random numbers
one period and three (P+3) integer numbers.
Below is given the Matlab code for the function. Stedents can generate random
numbers using different input parameters.
function [p, mean, var, oneperiod]=lcg(a, c, m, x)
% Linear Congruential Generators
% x the initial seed, 0 <= c < m
% a the multiplier, 0 <= a < m, normally greater than 1
% c the increment 0 <= c < m
% m the modulus, prime numbers are best
xseq=x;
for j=1:m+5 % generate m+5 integers
x= rem((a*x+c),m); % (a*x + c) mod m;
xseq=[xseq;x]; % concatenate numbers, in a cloumn
row=xseq'; % transpose to a row
end
% find out the period
i=1;
j=2;
while row(i) ~= row(j)
if j > m % integer row(i) doesnt repeat in the sequence
i=i+1; % see if the next integer repeats or not
j=i;
end;
j=j+1;
end;
p=j-i;
% one period of random integer number
oneperiod=row(i:i+p+2); % one period and three integers
% calculate the real number of a period p
r=row(i:i+p-1)/m; % divide by m, so distributed in [0,1)
mean = sum(r)/p; % average
diff=mean-r;
square=diff.*diff;
var=sum(square)/p; % variance
However, Matlab environment has already predefined functions to generate
random numbers:
RAND Uniformly distributed random numbers.
RAND(N) is an N-by-N matrix with random entries, chosen from a
uniform distribution on the interval (0.0,1.0)
RAND(M,N) and RAND([M,N]) are M-by-N matrices with random
entries
RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays
RAND with no arguments is a scalar whose value changes each time it is
referenced
RAND(SIZE(A)) is the same size as A
RANDN Normally distributed random numbers.
RANDN(N) is an N-by-N matrix with random entries, chosen from a
normal distribution with mean zero and variance one.
RANDN(M,N) and RANDN([M,N]) are M-by-N matrices with random
entries.
RANDN(M,N,P,...) or RANDN([M,N,P...]) generate random arrays.
RANDN with no arguments is a scalar whose value changes each time it is
referenced.
RANDN(SIZE(A)) is the same size as A.

SPRAND Sparse uniformly distributed random matrix.
R = SPRAND(S) has the same sparsity structure as S, but uniformly
distributed random entries.
R = SPRAND(m,n,density) is a random, m-by-n, sparse matrix with
approximately density*m*n uniformly distributed nonzero entries.
SPRAND is designed to produce large matrices with small density and will
generate significantly fewer nonzeros than requested if m*n is small or
density is large.
R = SPRAND(m,n,density,rc) also has reciprocal condition number
approximately equal to rc. R is constructed from a sum of matrices of rank
one.

SPRANDN Sparse normally distributed random matrix.
R = SPRANDN(S) has the same sparsity structure as S, but normally
distributed random entries.
R = SPRANDN(m,n,density) is a random, m-by-n, sparse matrix with
approximately density*m*n normally distributed nonzero entries.
SPRANDN is designed to produce large matrices with small density and
will generate significantly fewer nonzeros than requested if m*n is small or
density is large.
R = SPRANDN(m,n,density,rc) also has reciprocal condition number
approximately equal to rc. R is constructed from a sum of matrices of rank
one.
RANDPERM Random permutation.
RANDPERM(n) is a random permutation of the integers from 1 to n. For
example, RANDPERM(6) might be [2 4 5 6 1 3].


Last Updated: 21/10/2000
Valentin Muresan, Dublin City University, [email protected]

You might also like