Sie sind auf Seite 1von 3

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].

Das könnte Ihnen auch gefallen