# This function computes the probability that a poker hand contains # "nothing" (Exercise B5) by an exact method (a formula) and by a # simulation approximation. # The composition of the deck can be varied: nsuit is the number of # suits in the deck, nval is the number of card values, and nhand is # the number of cards in a hand. The usual situation is nsuit=4, # nval=13, nhand=5. In the simulation, the computer randomly deals # nrep hands of poker and determines what fraction of them contain # nothing. pnothing<-function(nsuit,nval,nhand,nrep){ ndeck<-nsuit*nval den<-choose(ndeck,nhand) nseq<-pmax(nval-nhand+1,0) PA<-choose(nval,nhand)*nsuit^nhand/den PAB<-nseq*nsuit^nhand/den PAC<-choose(nval,nhand)*nsuit/den PABC<-nseq*nsuit/den exact.ans<-PA-PAB-PAC+PABC tot<-0 deck<-cbind(rep(1:nsuit,rep(nval,nsuit)),rep(1:nval,nsuit)) hm1<-nhand-1 for(i in 1:nrep){ hand<-deck[sample(ndeck,nhand),] tot<-tot+((!anyDuplicated(hand[,2]))&&(diff(range(hand[,2]))!=hm1)&&(diff(range(hand[,1]))>0)) } sim.approx<-tot/nrep c(PA=PA,PAB=PAB,PAC=PAC,PABC=PABC,exact.ans=exact.ans,sim.approx=sim.approx) }