% Topic: Generating desired discrete random variables by using uniform random
% variable generator
% Case study: Making an unfair coin, and a dice  
% If you have any question, send an email to: asahebpa@uwaterloo.ca
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% See "The_game.m" before looking through this code 

%% An unfair coin
Probability_of_Head = 0.6
Probability_of_Tail = 1 - Probability_of_Head;
Number_of_trials = 100000; % Number of times which we toss the dice

for i = 1:Number_of_trials
    Uniform = rand(1);
    if Uniform < Probability_of_Head
        outcomecoin(i) = 'H';
    else 
        outcomecoin(i) = 'T';
    end
end
Probability_of_H_SimulatedCoin = length(find(outcomecoin=='H'))/Number_of_trials


%% A dice 
Number_of_trials = 100000; % Number of times which we roll the dice

for i = 1:Number_of_trials
    Uniform = rand(1);
    if Uniform < 1/6
        outcome(i) = 1;
    elseif 1/6 < Uniform && Uniform< 2/6
        outcome(i) = 2;
    elseif 2/6 < Uniform && Uniform< 3/6
        outcome(i) = 3;
    elseif 3/6 < Uniform && Uniform< 4/6
        outcome(i) = 4;
    elseif 4/6 < Uniform && Uniform < 5/6
        outcome(i) = 5;
    else
        outcome(i) = 6;
    end
end

Probability_of_6 = length(find(outcome==6))/Number_of_trials

