-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokerProblem.txt
More file actions
33 lines (28 loc) · 1.12 KB
/
PokerProblem.txt
File metadata and controls
33 lines (28 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# The Poker Test with Four Cards:
# Suppose you have four cards which each take on values 0 to 9 independently and with replacement.
# What is the probability of each of the following:
# 1. X cards are the same (i.e. 1 < X < = 4)
# 2. All cards are different (i.e. X = 1)
# 3. 2 pairs of cards that are the same, i.e. WWYY
# Code:
PT4 = function(n){ #n is the number of iterations
unique = 0 #X = 1 (e.g. [3,4,6,7])
one_pair = 0 #X = 2 (e.g. [3,3,9,7])
one_tri = 0 #X = 3 (e.g. [3,3,3,7])
two_pair = 0 #(e.g. [3,3,7,7])
for (i in 1:n) {
cards = sample(0:9,4,replace=T) #generate 4 cards with replacement
result = table(cards)
unique = unique + (length(result) == 4) #X = 1,the boolean value is 1 if true, 0 otherwise
one_pair = one_pair + (length(result) == 3)
one_tri = one_tri + (length(result) == 2 & max(result) == 3)
two_pair = two_pair + (length(result) == 2 & max(result) == 2)
}
prob = cbind(unique,one_pair,one_tri,two_pair) / n #generate the probability table
return(prob)
}
#Example:
n=5000
PT4(n)
# unique one_pair one_tri two_pair
# [1,] 0.5202 0.4162 0.031 0.0312