Question
MATLAB functions code dice game Lets see what we can figure out Start with one die. What are the possible outcomes from one roll? What
MATLAB functions code dice game
Lets see what we can figure out Start with one die.
What are the possible outcomes from one roll?
What is the most you would bet to win $100 on a 3 coming up in a single roll?
On a 6?
Rolling a die -1 What could a set of 6 rolls look like?
Set 1: [ 1, 2, 3, 4, 5, 6 ] ?
Set 2: [ 1, 1, 1, 1, 1, 1 ] ?
Set 3: [ 2, 4, 6, 1, 3, 5 ] ?
Set 4: [ 2, 2, 2, 3, 3, 3 ] ?
Is Set 3 the same as Set 1?
Rolling a die - 2
How to characterize the results of a set?
Lets agree that the order does not matter.
Lets do 12 rolls.
Heres a possible result: [ 6, 3, 5, 5, 1, 1, 4, 6, 6, 4, 5, 3 ]
Rolling a die - 3
How to characterize the results of a set?
Consider this result: [ 6, 3, 5, 5, 1, 1, 4, 6, 6, 4, 5, 3 ]
What is the mean of the set?
What would you expect the mean to be (if the die is fair)?
The idea of binning - 1
How to characterize the results of a set?
Consider this result: [ 6, 3, 5, 5, 1, 1, 4, 6, 6, 4, 5, 3 ]
Each roll outcome is between 1 and 6 (integer). Count the number of times each face occurs in the set; save count in the bin labeled 1,2, etc
The idea of binning - 2
Bin this result: [ 6, 3, 5, 5, 1, 1, 4, 6, 6, 4, 5, 3 ]
Bins: [ (1s), (2s), (3s), (4s), (5s), (6s) ]
Counts: [ 2, 0, 2, 2, 3, 3 ]
Do counts total to 12?
The idea of binning - 3
Bin this result: [ 6, 3, 5, 5, 1, 1, 4, 6, 6, 4, 5, 3 ]
Bin counts: [ 2, 0, 2, 2, 3, 3 ]
What is the mean based on bin counts?
Is any information lost in going from the roll set to the bin counts?
Test Case 1: one die, one set
1 set of rolls: nrolls = 12
Outcome: rollSet (1 x nrolls)
How to study the results?
o Mean of rolls: setMean
o Bin counts: bincount (1 x 6)
o Bin percentages: binpct (1 x 6)
Test Case 2: one die, three sets
3 sets of rolls:
nrolls1=12, nrolls2=120, nrolls3=1200
Outcomes:
rollSet1 (1 x nrolls1), rollSet2, rollSet3
How to study the results?
o Mean of rolls: setMean
o Bin counts: bincount (1 x 6)
o Bin percentages: binpct (1 x 6)
Test Case 2: one die, three sets
3 sets of rolls: nrolls1, rollSet1,
How to study the results?
o Array of means: setMeanarr (1x3)
o Bin counts: bincountarr (3x6)
o Bin percentages: binpctarr (3x6)
Test Case 3. Heaven ( pair o dice )
Uh-oh: lets think about this.
One roll of two dice: what are the possible outcomes?
[1,1], [1,2], [2,1], [1,3], [3,1],
Order doesnt matter, so sort by total.
Outcomes by totals: 2, 3, 4, , 11,12
Test Case 3. Heaven outcomes by totals 14
total =
2: 1+1
3: 1+2, 2+1
4: 1+3, 3+1, 2+2
5: 1+4, 4+1, 2+3, 3+2
6: 1+5, 5+1, 2+4, 4+2, 3+3
7: 1+6, 6+1, 2+5, 5+2, 3+4, 4+3
8: ?
9: ?
10: ?
11: 5+6, 6+6 12: 6+6
Test Case 3: two dice, one set of rolls
1 set of rolls: nrolls = 1100 (?)
Outcome: rollSet (1 x nrolls),
where a rollSet entry is the total of 2 dice.
How to study the results?
o Mean of rolls: setMean
o Bin counts: bincount (1 x 11)
o Bin percentages: binpct (1 x 11)
Test Case 4: Coin flip runs
New process: flip a (fair) coin.
Outcome: head or tail
One set of flips: nflips
Outcome: flipSet (1 x nflips)
A flipSet entry is 1 (head) or 2 (tail).
Example: flipSet= [ 1,2,2,2,1, 2,1,1,1,1]
Test Case 4: Coin flip runs
Example: flipSet= [ 1,2,2,2,1, 2,1,1,1,1]
Characterize results:
setMean: (expected value?)
setRuns: [ 1, 3, 1, 1, 4 ]
binRuns: [ 3, 0, 1, 1 ]
--------
function [setMean1,bincount1,binpct1]= dice_1_fcn(nrolls1,rollSet1)
% id: sec00?, your folder name
% INPUT definitions:
% nrolls1, number of rolls
% rollSet1, roll results (1 x nrolls)
% OUTPUT definitions:
% bincount1, nbr of results in each bin
% binpct1, percent of results in each bin
% initialize outputs
bincount1= zeros(1,6);
binpct1= zeros(1,6);
(Your outstanding solution goes in here)
end
-------------------------------------------------------------------------------
Test case 2 (3 pts). Three sets of rolls of one die.
--------
function [setMean2arr,bincount2arr,binpct2arr]= dice_2_fcn( nrolls21,rollSet21,nrolls22,rollSet22,nrolls23,rollSet23]
% id: sec00?, your folder name
% INPUT definitions:
% nrolls21, number of rolls for set 1
% rollSet21, roll results for set 1 (1 x nrolls21)
% OUTPUT definitions:
% setMean2arr, mean of each roll set (1 x 3)
% bincount2arr, bincount array for each set (3 x 6)
% binpct2arr, binpct array for each set (3 x 6)
%
% initialize outputs
setMean2arr= zeros(1 x 3);
bincount2arr= zeros(3 x 6);
binpct2arr= zeros(3 x 6)
Test case 3 (2 pts). One set of rolls of two dice.
--------
function [setMean3,bincount3,binpct3]= dice_3_fcn(nrolls3,rollSet3)
% id: sec00?, your folder name
% INPUT definitions:
% nrolls3, number of rolls for set 1
% rollSet3, roll results (total of faces) for set 1 (1 x nrolls3)
% OUTPUT definitions:
% setMean3, mean of roll set
% bincount3, bincount array for roll set (1 x 11)
% binpct3, binpct array for roll set (1 x 11)
%
% initialize outputs
bincount3= zeros(1 x 11);
binpct3= zeros(1 x 11)
(Your outstanding solution goes in here)
end
--------------------------------------------------------------------------------
Test case 4 (2 pts). Two set of rolls of pair of fair dice.
Find differences between bin percentages and expected bin percentages for two sets of rolls of a pair of fair dice.
--------
function [binpct41,binpctdiff41,binpct42,binpctdiff42]= ... dice_4_fcn(nrolls41,rollSet41,nrolls42,rollSet42); %
id: sec00?, your folder name
% INPUT definitions:
% nrolls41, number of rolls in set 41
% rollSet41, roll results (1 x nrolls41)
% nrolls42, number of rolls in set 42
% rollSet42, roll results (1 x nrolls42)
% OUTPUT definitions:
% binpct41, bin percentages (1 x 11)
% binpctdiff41, (binpct41 - bin expected value) (1 x 11)
% binpct42, bin percentages (1 x 11)
% binpctdiff42, (binpct42 - bin expected value) (1 x 11)
% initialize outputs
binpct41= zeros(1,11);
binpctdiff41= zeros(1,11);
binpct42= zeros(1,11);
binpctdiff42= zeros(1,11);
% -------------------------------------------------------------------------
% Test case 1: One set of rolls of one die.
% Test case 2: Three sets of rolls of one die.
% Test case 3: One set of rolls of two dice.
% Test case 4: One set of flips of coin. Study of runs.
% -------------------------------------------------------------------------
clc clear
disp(' ');
mfilename= 'dice_test_1_demo.m'
% -------------------------------------------------------------------------
% Solution setup and solve ------------------------------------------------
% TC1 -- One set of rolls of one die. ------------------------------------
% IN: nrolls1, number of rolls
% IN: rollSet1, roll results (1 x nrolls)
% OUT: setMean1, mean of roll set
% OUT: bincount1, nbr of results in each bin
% OUT: binpct1, percent of results in each bin
nrolls1= 12;
rollSet1= zeros(1,nrolls1);
for nrx=1:1:nrolls1
rollSet1(1,nrx)= randi(6); % generate rollSet1
end
[setMean1,bincount1,binpct1]= dice_1_fcn_ans(nrolls1,rollSet1);
disp(' ');
disp('Test TC1:');
fprintf('nrolls1= %4.0f ',nrolls1);
fmt11= ['setRolls1(1,1:12)= [',repmat('%3.0f',1,12),'] '];
fprintf(fmt11,rollSet1(1,1:12));
fprintf('setMean1= %5.2f ',setMean1);
fmt12= ['bincount1= [',repmat('%4.0f',1,6),'] '];
fprintf(fmt12,bincount1(1,1:6));
fmt13= ['binpct1= [',repmat('%6.2f',1,6),'] '];
fprintf(fmt13,binpct1(1,1:6));
ax11= subplot(2,1,1);
y11= bincount1(1,1:6);
bar(ax11,y11)
title('bin counts for 1 set of 12 rolls');
ax12= subplot(2,1,2);
y12= binpct1(1,1:6);
bar(ax12,y12)
title('bin pcts for 1 set of 12 rolls');
%pause
% eof -
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started