probability - Simulate experiment with R Programming -
i new in r programming. need solve 1 problem in r. need simulate following experiment in r. poker hand consists of 5 cards dealt conventional pack of 52 cards, order of cards not being important. find probability given hand has @ least 1 king , @ least 1 queen.
i know how find atleast 1 king not @ least 1 king , @ least 1 queen. atleast 1 king code :
deck<- rep(1:13 , each=4) #here j=11 , q=12, k=13 nhands <- 1000 xk<- c(rep(-1, nhands)) for( in 1:nhands){ hand <- sample( deck , 5 , replace= false) numberofk<-0 for( j in 1:5){ # count kings if( hand[j] == 13){ numberofk <- numberofk +1 } } #print(numberofk) xk[i] <-numberofk #print(hand) } table(xk) /nhands
can please me in coding required 1.. thanks
the probability of hand of 5
cards contain @ least 1 king or 1 queen can written following, sample(deck, 5)
gives hand of 5
cards while any(c(12, 13) ...)
checks whether king
or queen
within hand , sum
counts how many times such case happens within 1000 simulation:
set.seed(10) sum(sapply(1:100000, function(i) { any(c(12, 13) %in% sample(deck, 5))}))/100000 # [1] 0.58365
theoretically, probability of such case be:
(choose(52, 5) - choose(44, 5))/choose(52, 5) # [1] 0.5821375
which pretty close.
and on other hand, if indeed and means @ least king , queen, simulation gives:
set.seed(10) sum(sapply(1:100000, function(i) { all(c(12, 13) %in% sample(deck, 5))}))/100000 # [1] 0.09932
and theoretically:
(choose(52, 5) - choose(44, 5) - (2*(choose(48, 5) - choose(44, 5))))/choose(52, 5) # [1] 0.1001785
and number matches closely.
Comments
Post a Comment