# Mark chosen
tag @e[tag=pirate_barrack,tag=!choosen,sort=nearest,limit=1] add choosen
function rng:percentage
execute if score out rng matches 40.. run tag @e[tag=pirate_barrack,tag=!choosen,sort=nearest,limit=1] add choosen
execute if score out rng matches 80.. run tag @e[tag=pirate_barrack,tag=!choosen,sort=nearest,limit=1] add choosen

# Summon
execute at @e[tag=pirate_barrack,tag=choosen] run function bosses:pirate_captain/m_ghost/call

# Clear
tag @e[tag=pirate_barrack,tag=choosen] remove choosen
tag @s remove mGhost
tag @s remove attacking
