Minecraft – How to randomly select every second/third entity of a kind

minecraft-commandsminecraft-java-edition

I have decided that I don't like standing in front of a mob spawner for quite a while in order to reach level 30. Seeing that I don't (yet) have an Enderman farm (or a Pigman farm), I wondered if I can't simply increase the XP I get from everything by about 50% using command block magic to summon more of them.

The page for Experience states that most XP orbs have values of 1, 3, or 7, and I'll limit my contraption to working with these three.

I have set up a scoreboard objective to distinguish freshly dropped XP orbs and their value using

scoreboard players set @e[type=XPOrb] XP 1 {Age:10s,Value:1s}

I would like to either summon a copy of these orbs at their position, or simply double their value, but only for about half of them, which brings me to my question:

How can I randomly select only half of them for use with execute -> summon or entitydata? Ideally, this should be random on an entity basis, rather than "globally" (i.e. using time%2), in order to make the increase smoother.

Best Answer

One way to do this could be having each new XP orb summon an XP orb with twice the XP value at its location, and then having the original XP orb randomly kill exactly one entity at its position. This would give 1/2 probability of leaving the original orb, and 1/2 of leaving the newer increased value orb.

On a clock, for XP orbs with a value of 1:

/execute @e[type=XPOrb,score_XP=1,score_XP_min=1,tag=!MRXPIncreasedOrb] ~ ~ ~ /summon XPOrb ~ ~ ~ {Tags:["MRXPIncreasedOrb"],Value:2s}
/execute @e[type=XPOrb,score_XP=1,score_XP_min=1,tag=!MRXPIncreasedOrb] ~ ~ ~ /kill @r[type=XPOrb,r=0]

Repeat for all orb values you want to work. For example, for 3:

/execute @e[type=XPOrb,score_XP=3,score_XP_min=3,tag=!MRXPIncreasedOrb] ~ ~ ~ /summon XPOrb ~ ~ ~ {Tags:["MRXPIncreasedOrb"],Value:6s}
/execute @e[type=XPOrb,score_XP=3,score_XP_min=3,tag=!MRXPIncreasedOrb] ~ ~ ~ /kill @r[type=XPOrb,r=0]

I'm not sure how it's set up at the moment, but if you're not already doing so you should remove the XP orb's XP score after they get processed, so they aren't processed again:

/scoreboard players reset @e[type=XPOrb,score_XP_min=0] XP

For 1/3 chance rather than 1/2, have another XP orb summoned but with the same value rather than twice the value, and add c=2 to the @r selector so 2 of the orbs are killed.