# 乱射狙撃手
rm(list=ls())
RandomDuel <- function(
pa=1/3, # probability of death
pb=1/2,
pc=1,
k=1e5){
ps=c(1-pa,1-pb,1-pc) # probability of survival
f32 <- function(){
abc=c(1,1,1) # survival probability
shop=c(0,0,0) # shooting priority for A,B,C
while(sum(abc)==3){ # while 3 suviors
bc=c(2,3) # A shoots B or C
i=sample(1:2,1) # index of target bc
alive=abc[bc[i]]=rbinom(1,1,ps[1]) # target alive or dead
if(alive==0){ # if either killed
shop[bc[-i]]=1 # the survivor gets shooting priority
break
}
ca=c(3,1) # B shoots C or A
i=sample(1:2,1) # index of target ca
alive=abc[ca[i]]=rbinom(1,1,ps[2]) # target alive or dead
if(alive==0){ # if either killed
shop[ca[-i]]=1 # the survivor gets shooting priority
break
}