%Monte Carlo simulation sampling notes
%sampling_notes.m    VERSION 1.0      7 January 2014

%% seed random number generator to state zero (debugging and development)
%  utility function: seedRNG0.m
if verLessThan('matlab', '8.0.0')
    RandStream.setDefaultStream(RandStream('mt19937ar','seed',0));
else
    RandStream.setGlobalStream(RandStream('mt19937ar','seed',0));
end


%% seed random number generator using system date/time (simulation)
%  utility function: seedRNG.m
if verLessThan('matlab', '8.0.0')
    RandStream.setDefaultStream(RandStream('mt19937ar','seed',sum(clock*100)));
else
    RandStream.setGlobalStream(RandStream('mt19937ar','seed',sum(clock*100)));
end


%% draw N samples from rectangular distribution ranging from 0.0 - 1.0
N=5; sample = rand(N,1)


%% generate N values drawn from uniform distribution in the range of [a, b]
N=10; a=1; b=10;
sample = a + (b-a) .* rand(N,1)
%
%round results to the nearest whole number
sample = floor(sample + 0.5)


%% generate random integers from rectangular discrete distribution
%  ranging from 1 to iMax
iMax=10;  nSamples=5;
sample = randi(iMax, nSamples, 1)


%% generate random numbers drawn from the standard normal distribution
nSamples = 5;  sample = randn(nSamples,1)
%
% generate random numbers drawn gaussian dist(mean=mu, std dev=sigma)
mu=0; sigma=1; nSamples=5;
sample = mu + sigma .* randn(nSamples,1)


%% draw a sample from a normal distribution of specified mean; std dev
mu=100; sigma=15; nSamples=15;
sample = normrnd(mu, sigma, nSamples, 1)


%% draw a random sample from a finite, user-defined population
%  (with or without replacement)
population = [0 0 0 0 0 0 0 0 0 1 1]; nSamples=10;
bReplacement = true; %specifiy sampling with replacement
sample = randsample(population, nSamples, bReplacement)
%
% draw samples from a population of amino acids (i.e., 'ACGT') and
% specify the a priori probability of each member of the population
mySequence = randsample('ACGT', 24, true, [0.15 0.35 0.35 0.15])


%% generate a random permutation for the numbers 1 through N
N=10;
sample = randperm(N)


%% shuffle the order of the elements in a row vector
data = [10 20 30 40 50 60 70 80 90 100]
order = randperm( length(data) )
shuffled_data = data(order)


%% Roll a die
outcome = randi([1 6],1)


%% Flip a coin
outcome = rand < 0.5
%or
outcome = randi([0 1],1)
%
%flip a coin N times
N=10;
outcome = rand(N,1) < 0.5
%or
outcome = randi([0 1],N,1)
