Project 1
Purpose:
Understanding the Matlab "hist" function, Duplicating the results of the probability of the
sum of two dice for a user definable number of rolls. When 100 coins are tossed finding
the probability that exactly 50 will be heads, and determining the probability of "4 of a
kind" in a 5-card poker draw
1/
Matlab Code:
nbseven=0;
nbtimes=0;
bins=[1:1:100];
for f=1:1:100
for t=1:1:100
sum=randi(6)+randi(6);
if(sum==7)
bins(f)=t;
break
end
end
end
x=0:20;
[yvalues,xvalues]=hist(bins,x);
bar(xvalues,yvalues)
2/ Theoretically:
P (50 heads)= 100C50 / 2^100 =0.0796
Numerical Answer:
theads =
Columns 1 through 14
45
49
54
47
54
53
45
58
54
45
40
50
56
49
53
42
50
51
55
50
51
51
49
Columns 15 through 28
52
54
53
47
57
Columns 29 through 42
51
42
47
62
53
50
49
43
44
48
50
53
51
53
37
51
48
52
50
57
52
53
45
50
54
55
62
45
49
46
45
48
45
52
54
47
47
49
43
54
42
49
52
53
60
53
43
48
49
47
35
36
37
38
39
40
41
42
43
49
50
51
52
53
54
55
56
57
63
64
65
66
67
68
69
70
Columns 43 through 56
54
52
52
49
59
Columns 57 through 70
62
48
53
45
55
Columns 71 through 84
52
52
43
54
48
Columns 85 through 98
49
45
50
46
42
Columns 99 through 100
48
49
probability =
0.0800
x=
Columns 1 through 14
30
31
32
33
34
Columns 15 through 28
44
45
46
47
48
Columns 29 through 41
58
59
60
Matlab Code:
61
62
nbfifty=0;
thheads=[0:0:100];
heads=0;
nbtrials=100;
for table=1:1:100
heads=0;
for t=1:1:100
coin=randi(2)-1;
if(coin==1)
heads=heads+1;
end
end
theads(table)=heads;
if(heads==50)
nbfifty=nbfifty+1;
end
end
probability=nbfifty/100;
theads
probability
x=30:70
[yvalues,xvalues]=hist(theads,x);
bar(xvalues,yvalues);
Graphical answer:
Using bar
Using Stem
3/ Numerical answer:
8888Q
AAAAQ
prob =
2.0000e-04
Matlab Code:
CARDS = ['AH';'2H';'3H';'4H';'5H';'6H';'7H';'8H';'9H';'TH';'JH';'QH';'KH';...
'AS';'2S';'3S';'4S';'5S';'6S';'7S';'8S';'9S';'TS';'JS';'QS';'KS'; ...
'AD';'2D';'3D';'4D';'5D';'6D';'7D';'8D';'9D';'TD';'JD';'QD';'KD';
...
'AC';'2C';'3C';'4C';'5C';'6C';'7C';'8C';'9C';'TC';'JC';'QC';'KC'];
nb_runs=10000;
n=0;
for i=1:nb_runs
ind=randperm(52);
shuffle=CARDS(ind);
x=shuffle(1:5);
c=sort(x);
if c(2)==c(5) ||c(1)==c(4)
disp(c);
n=n+1;
end
end
probability=n/nb_runs