Here is the code written in R. Note, I am not a computer programmer by any means and sort of cobbled this together. I coded the 52-card deck from A-2 in spades, hearts, diamonds, and clubs. Ranks are given by the rlabel vector and suits go from 1 to 4. In the code, I assume suits of the original three card badugi are spades, hearts, and diamonds, respectively, from high to low.
Code:
badugi3=function(trials)
{
set.seed(1234)
rlabels=c(1,13,12,11,10,9,8,7,6,5,4,3,2)
vec52=1:52
vec3=1:3
veccy=c(100,10,1)
tally=rep(0,20)
tallyvec=rep(0,20)
tallyvec[1]=654
tallyvec[2]=653
tallyvec[3]=652
tallyvec[4]=651
tallyvec[5]=643
tallyvec[6]=642
tallyvec[7]=641
tallyvec[8]=632
tallyvec[9]=631
tallyvec[10]=621
tallyvec[11]=543
tallyvec[12]=542
tallyvec[13]=541
tallyvec[14]=532
tallyvec[15]=531
tallyvec[16]=521
tallyvec[17]=432
tallyvec[18]=431
tallyvec[19]=421
tallyvec[20]=321
cardmat=matrix(0,nrow=20,ncol=3)
cardmat[1,]=c(9,23,37)
cardmat[2,]=c(9,23,38)
cardmat[3,]=c(9,23,39)
cardmat[4,]=c(9,23,27)
cardmat[5,]=c(9,24,38)
cardmat[6,]=c(9,24,39)
cardmat[7,]=c(9,24,27)
cardmat[8,]=c(9,25,39)
cardmat[9,]=c(9,25,27)
cardmat[10,]=c(9,26,27)
cardmat[11,]=c(10,24,38)
cardmat[12,]=c(10,24,39)
cardmat[13,]=c(10,24,27)
cardmat[14,]=c(10,25,39)
cardmat[15,]=c(10,25,27)
cardmat[16,]=c(10,26,27)
cardmat[17,]=c(11,25,39)
cardmat[18,]=c(11,25,27)
cardmat[19,]=c(11,26,27)
cardmat[20,]=c(12,26,27)
#for (j in 1:20)
#{
# print(rlabels[ranks(cardmat[j,])])
#}
count=0
for (t in 1:(10*trials))
{
rand=sample(20,1)
startcards=cardmat[rand,]
startranks=rlabels[ranks(startcards)]
remaincards=setdiff(vec52,startcards)
#deal 4th card
deal4card=2 # king of spades
remaincards=setdiff(remaincards,deal4card)
#first draw
newranks=startranks
firstnewcard=sample(remaincards,1)
firstnewsuit=suits(firstnewcard)
firstnewrank=rlabels[ranks(firstnewcard)]
if(firstnewsuit<4)
{
othersuits=setdiff(vec3,firstnewsuit)
if((firstnewrank<startranks[firstnewsuit])&(!(firstnewrank==startranks[othersuits[1]]))&(!(firstnewrank==startranks[othersuits[2]])))
{
#browser()
newranks[firstnewsuit]=firstnewrank
}
#second draw
remaincards=setdiff(remaincards,firstnewcard)
secondnewcard=sample(remaincards,1)
secondnewsuit=suits(secondnewcard)
secondnewrank=rlabels[ranks(secondnewcard)]
if(secondnewsuit<4)
{
othersuits=setdiff(vec3,secondnewsuit)
if((secondnewrank<newranks[secondnewsuit])&(!(secondnewrank==newranks[othersuits[1]]))&(!(secondnewrank==newranks[othersuits[2]])))
{
#browser()
newranks[secondnewsuit]=secondnewrank
}
sortnewranks=sort(newranks, decreasing=TRUE)
sorty=sum(veccy*sortnewranks)
tw=which(tallyvec==sorty)
tally[tw]=tally[tw]+1
count=count+1
if(count==trials) break
}
}
}
return(cbind(tallyvec,tally))
}