%script to simulate the problem of a miner trapped in a mine
%with 3 doors to selected. One door leads to freedom in 3 hrs,
%second door leads back to the mine in 5 hrs, 3rd door leads
%back to the mine in 7 hrs.  Find expected time to leave the mine

%by Nasser Abbasi. For math 502 CSUF probability and statistics 10/18/07

%example output:
%
%number of simulations=20000, Time to escape = 15.054550

%----- configuration section ----------%

TRUE  = 1; 
FALSE = 0;
total_simulation_time = 0;

t1 = 3;   %time to escape for first door
t2 = 5;   %time to escape for second door
t3 = 7;   %time to escape for third door

number_of_simulations     = 20000;
current_simulation_number = 0;
number_of_doors = 3;
randn('state',010101);

%------ algorithm start --------------------%

while current_simulation_number < number_of_simulations
    this_simulation_time = 0;
    escaped = FALSE;
    
    while not(escaped)
        door_number = ceil(number_of_doors.*rand(1,1));
        switch door_number
            case 1
                escaped = TRUE;
                this_simulation_time = this_simulation_time + t1;
            case 2
                this_simulation_time = this_simulation_time + t2;
            case 3
                this_simulation_time = this_simulation_time + t3;
        end
    end
    
    total_simulation_time = total_simulation_time + this_simulation_time;
    current_simulation_number = current_simulation_number + 1;
end

fprintf('\nNumber of simulations=%d\nTime to escape = %f\n',...
    number_of_simulations,...
    total_simulation_time/number_of_simulations  );
