--- /dev/null
+function [dice_results] = dice_sim(num_of_dice)
+%dice_sim Simulates ´num_of_dice´ number of rolls
+% OUTPUT: ´dice_results´ 1-dimensional vector of all rolls
+ dice_results = randi(6, 1, num_of_dice);
+end
+
+
+function [sum_vec] = organize_dice(dice_results)
+%organize_dice Organizes ´dice_results´ by their value
+% OUTPUT: ´sum_vec´ 1-dimensional summary vector, dice index corresponds
+% to dice value
+ sum_vec = zeros(1, 6);
+ for num=1:6
+ for i=1:length(dice_results)
+ if dice_results(i) == num
+ sum_vec(num) = sum_vec(num) + 1;
+ end
+ end
+ end
+end
+
+
+function [count] = toss_til_yahtzee(dice_total)
+%toss_til_yahtzee Performs dice rolls with ´dice_total´ number of dice until yahtzee
+% OUTPUT: ´count´ number of rolls until yahtzee
+ count = 1;
+ % Initial roll
+ results = dice_sim(dice_total);
+ [main_n, main_most_common] = max(organize_dice(results));
+
+ while main_n < dice_total
+
+ % Continue rolling until all dice equal
+ results = dice_sim(dice_total - main_n);
+ sigma = organize_dice(results);
+ [n, most_common] = max(sigma);
+
+ % If rolled dice contain more matching values than saved dice,
+ % switch.
+ if n > main_n
+ main_most_common = most_common;
+ main_n = n;
+ else
+ main_n = main_n + sigma(main_most_common);
+ end
+ count = count + 1;
+ end
+end
+
+
+function [mhat, s2hat] = yatzy(experiments)
+%yatzy Rolls dice until yahtzee ´experiments´ number of times, plots the data
+% and calculates statistical properties of the experiment.
+% OUTPUT: ´mhat´ average number of rolls til yahtzee
+% OUTPUT: ´s2hat´ variance in dice rolls required for yahtzee
+ dice_total = 5;
+ results = zeros(experiments, 1);
+
+ % Do experiment
+ for i=1:experiments
+ results(i) = toss_til_yahtzee(dice_total);
+ if mod(i, 1000) == 0
+ % Status update
+ fprintf('%d%% Done\n',i/(experiments/100));
+ end
+ end
+
+ % Matrices for statistics calculations.
+ A = [
+ 0 1/6 1/36 1/216 1/1296
+ 0 5/6 10/36 15/216 25/1296
+ 0 0 25/36 80/216 250/1296
+ 0 0 0 120/216 900/1296
+ 0 0 0 0 120/1296
+ ];
+ e_1 = [1;0;0;0;0];
+ e_5 = [0;0;0;0;1];
+
+ % Statistics y values for plotting. Only 50 up to 50 total rolls
+ % plotted.
+ chance = zeros(50,1);
+ for i=1:50
+ chance(i, 1) = e_1.' * A^i * e_5;
+ end
+
+ % Clear figure and plot data, histogram for experiment and dashed line
+ % for theoretical statistics
+ clf;
+ histogram(results);
+ title("Number of dice toss to get yahtzee!")
+ ylabel("Number of attempts")
+ xlabel("Rolls")
+ hold on;
+ yyaxis right;
+ ylabel("Theoretical percentage")
+ plot([1:50], chance, linewidth=3, LineStyle='--');
+ legend("Number of attempts", "Theoretical percentage");
+
+ % Statistics calculations
+ mean_ = mean(results);
+ variance = sum((results - mean(results)).^2)/(length(results) - 1);
+ fprintf('Mean: %d\n',mean_);
+ fprintf('Variance: %d\n',variance);
+ mhat = mean_;
+ s2hat = variance;
+end
+
+
+
+% ------------------EXAMPLE-------------------
+% Run the experiment by running the main function yatzy(experiments), all other functions should be accessible to yatzy(experiments)
+% >>
+[mhat, s2hat] = yatzy(10000)
\ No newline at end of file
function [dice_results] = dice_sim(num_of_dice)
-%dice_sim Simulates ´num of dice´ many dice rolls and returns results vector
-dice_results = randi(6, 1, num_of_dice);
+%dice_sim Simulates ´num_of_dice´ number of rolls
+% OUTPUT: ´dice_results´ 1-dimensional vector of all rolls
+ dice_results = randi(6, 1, num_of_dice);
end
\ No newline at end of file
+++ /dev/null
-histogram(dice_results, 6)
\ No newline at end of file
+++ /dev/null
-clear;
-clc;
-
-% Roll x dice
-count = 1;
-results = dice_sim(5);
-[main_n, main_most_common] = max(sum_vec(results));
-main_most_common
-results
-main_n
-while count < 5
- results = dice_sim(5 - main_n);
- % determine most common dice
-
- [n, most_common] = max(sum_vec(results));
- if n > main_n
- main_most_common = most_common;
- main_n = n;
- else
- if most_common
- main_n = main
-
- end
- main_most_common
- results
- main_n
- % reroll other dice
- % repeat until all dice are unified
- count = count + 1;
-end
+++ /dev/null
-clear;
-clc;
-dice = 10;
-% Roll x dice
-count = 1;
-results = dice_sim(dice);
-[main_n, main_most_common] = max(sum_vec(results));
-while main_n < dice
- results = dice_sim(dice - main_n);
- % determine most common dice
- sigma = sum_vec(results);
- [n, most_common] = max(sigma);
- if n > main_n
- main_most_common = most_common;
- main_n = n;
- else
- main_n = main_n + sigma(main_most_common);
- end
- % reroll other dice
- % repeat until all dice are unified
- count = count + 1;
-end
-count
\ No newline at end of file
--- /dev/null
+function [sum_vec] = organize_dice(dice_results)
+%organize_dice Organizes ´dice_results´ by their value
+% OUTPUT: ´sum_vec´ 1-dimensional summary vector, dice index corresponds
+% to dice value
+ sum_vec = zeros(1, 6);
+ for num=1:6
+ for i=1:length(dice_results)
+ if dice_results(i) == num
+ sum_vec(num) = sum_vec(num) + 1;
+ end
+ end
+ end
+end
\ No newline at end of file
+++ /dev/null
-function [sum_vec] = sum_vec(dice_results)
-%UNTITLED5 Summary of this function goes here
-% Detailed explanation goes here
-sum_vec = zeros(1, 6);
-for num=1:6
- for i=1:length(dice_results)
- if dice_results(i) == num
- sum_vec(num) = sum_vec(num) + 1;
- end
- end
-end
\ No newline at end of file
--- /dev/null
+function [count] = toss_til_yahtzee(dice_total)
+%toss_til_yahtzee Performs dice rolls with ´dice_total´ number of dice until yahtzee
+% OUTPUT: ´count´ number of rolls until yahtzee
+ count = 1;
+ % Initial roll
+ results = dice_sim(dice_total);
+ [main_n, main_most_common] = max(organize_dice(results));
+
+ while main_n < dice_total
+
+ % Continue rolling until all dice equal
+ results = dice_sim(dice_total - main_n);
+ sigma = organize_dice(results);
+ [n, most_common] = max(sigma);
+
+ % If rolled dice contain more matching values than saved dice,
+ % switch.
+ if n > main_n
+ main_most_common = most_common;
+ main_n = n;
+ else
+ main_n = main_n + sigma(main_most_common);
+ end
+ count = count + 1;
+ end
+end
\ No newline at end of file
--- /dev/null
+function [mhat, s2hat] = yatzy(experiments)
+%yatzy Rolls dice until yahtzee ´experiments´ number of times, plots the data
+% and calculates statistical properties of the experiment.
+% OUTPUT: ´mhat´ average number of rolls til yahtzee
+% OUTPUT: ´s2hat´ variance in dice rolls required for yahtzee
+ dice_total = 5;
+ results = zeros(experiments, 1);
+
+ % Do experiment
+ for i=1:experiments
+ results(i) = toss_til_yahtzee(dice_total);
+ if mod(i, 1000) == 0
+ % Status update
+ fprintf('%d%% Done\n',i/(experiments/100));
+ end
+ end
+
+ % Matrices for statistics calculations.
+ A = [
+ 0 1/6 1/36 1/216 1/1296
+ 0 5/6 10/36 15/216 25/1296
+ 0 0 25/36 80/216 250/1296
+ 0 0 0 120/216 900/1296
+ 0 0 0 0 120/1296
+ ];
+ e_1 = [1;0;0;0;0];
+ e_5 = [0;0;0;0;1];
+
+ % Statistics y values for plotting. Only 50 up to 50 total rolls
+ % plotted.
+ chance = zeros(50,1);
+ for i=1:50
+ chance(i, 1) = e_1.' * A^i * e_5;
+ end
+
+ % Clear figure and plot data, histogram for experiment and dashed line
+ % for theoretical statistics
+ clf;
+ histogram(results);
+ title("Number of dice toss to get yahtzee!")
+ ylabel("Number of attempts")
+ xlabel("Rolls")
+ hold on;
+ yyaxis right;
+ ylabel("Theoretical percentage")
+ plot([1:50], chance, linewidth=3, LineStyle='--');
+ legend("Number of attempts", "Theoretical percentage");
+
+ % Statistics calculations
+ mean_ = mean(results);
+ variance = sum((results - mean(results)).^2)/(length(results) - 1);
+ fprintf('Mean: %d\n',mean_);
+ fprintf('Variance: %d\n',variance);
+ mhat = mean_;
+ s2hat = variance;
+end
\ No newline at end of file