%This is a sample code to show how to generate a sequence of exponential
%random variables. The sole purpose of this code is to time how long the
%procedure takes.
%The inputs:
% N = the number of random numbers I want. This will be 100,000
% based upon the problem I gave in the homework.
% lambda = the parameter for the exponential random variable.
% Based on the problem I assigned, this will be 1/10.
%The ouput:
% cputime = The needed CPU runtime to complete the problem.
%How to call this function: put this script into the folder in which
%Matlab is open. Then type the following into Matlab:
% [cputime] = Exponential(100000,1/10);
function [cputime] = Exponential(N,lambda)
%This sets the "clock" to "bt", which we can later use as a "start time".
bt = clock;
%I am storing the data, which is not actually necessary for this example.
%The command zeros([a,b]), creates an array (or matrix) of all zeros that
%is of size a X b. I am making a single column vector to hold the data.
storage = zeros([N,1]);
%The main FOR LOOP. It runs from 1 through N, generating a single
%exponential random variable of parameter lambda at each step. It is
%important to note that calling exprnd(mu) returns an exponential random
%variable with a mean of mu, and not 1/mu, as presented in class.
%Thus, they have inverted the parameter compared with how I presented it.
for j = 1:N
storage(j) = exprnd(1/lambda);
end
cputime = etime(clock,bt)
%I like to output the mean and variance. This is just a check to make
% sure nothing went wrong. Of course, simply having the correct mean and
% variance does not guarantee nothing went wrong.
mean(storage)
var(storage)
end