3
Help with randomness
I'm having difficulty with randomness in a function I'm working on.
Let's say I'd like to include a command in my function that does some action, but only randomly 25% of the times my function runs (as opposed to the typical 100%).
To accomplish this, I'm trying the idea of summoning 4 invisible armor stands - one has a tag "pick_me", and the other 3 have "dont_pick_me". I then use random entity target selection to randomly pick one of the stands; if the chosen stand has the "pick_me" tag, then boom - do some action.
The problem is that the action isn't happening. In my case, it's to add a tag to all players. Here's what I have:
In the function, executed as a player:
Players never end up getting tagged with "yay_it_worked". Anyone see what I messed up?
Let's say I'd like to include a command in my function that does some action, but only randomly 25% of the times my function runs (as opposed to the typical 100%).
To accomplish this, I'm trying the idea of summoning 4 invisible armor stands - one has a tag "pick_me", and the other 3 have "dont_pick_me". I then use random entity target selection to randomly pick one of the stands; if the chosen stand has the "pick_me" tag, then boom - do some action.
The problem is that the action isn't happening. In my case, it's to add a tag to all players. Here's what I have:
In the function, executed as a player:
execute as @s run summon minecraft:armor_stand ~-1 ~2 ~-1 {Tags:["dont_pick_me"],Invisible:1,Invulnerable:1,NoGravity:1}
execute as @s run summon minecraft:armor_stand ~-1 ~2 ~1 {Tags:["dont_pick_me"],Invisible:1,Invulnerable:1,NoGravity:1}
execute as @s run summon minecraft:armor_stand ~1 ~2 ~-1 {Tags:["dont_pick_me"],Invisible:1,Invulnerable:1,NoGravity:1}
execute as @s run summon minecraft:armor_stand ~1 ~2 ~1 {Tags:["pick_me"],Invisible:1,Invulnerable:1,NoGravity:1}
execute as @e[type=minecraft:armor_stand,sort=random,limit=1] if entity @s[tag=pick_me] run tag @a add yay_it_workedPlayers never end up getting tagged with "yay_it_worked". Anyone see what I messed up?
3
There are a few problems with this code:
- You execute the first four commands as @s, which means the command is executed as the player who executed the command, which is true nonetheless. You probably meant to write at @s which triggers the command where the player stands but this is also true anyway, so it would be best to just remove the execute command.
- You summon every armor stand in different coordinates, which doesn't contribute anything.
- Your function doesn't have to do anything with the tag dont_pick_me. Remove it, it's worthless.
- Invisible armor stands are always invulnerable, remove the Invulnerable:1.
- You choose a random armor stand among all, which means putting an armor stand can affect the chances. Give all armor stands (both pick_me and dont_pick_me) another tag (e.g. random) and check for it in the target selector (tag=random).
- You tag all players on success. Change @a to @s.
- You check all armor stands, which means if two players execute the function at the same time the chances get worse. Use distance=..0.5 to only check near armor stands.
- You don't kill the armor stands, which means the chances get worse with every execute of the function.
summon minecraft:armor_stand ~ ~ ~ {Tags:["random"],Invisible:1,NoGravity:1}
summon minecraft:armor_stand ~ ~ ~ {Tags:["random"],Invisible:1,NoGravity:1}
summon minecraft:armor_stand ~ ~ ~ {Tags:["random"],Invisible:1,NoGravity:1}
summon minecraft:armor_stand ~ ~ ~ {Tags:["random","pick_me"],Invisible:1,NoGravity:1}
execute as @e[tag=random,sort=random,limit=1,distance=..0.5] if entity @s[tag=pick_me] run tag @s add yay_it_worked
kill @e[tag=random,distance=..0.5]This also has some bugs, but those are very minor. But actually... there is something in Minecraft called predicates, which can be used to check a random chance. Here is the predicate you need (put it inside <your_datapack_folder>/data/<your_namespace>/predicates/random_chance.json):{
"condition": "random_chance",
"chance": 0.25
}Then your function is really small:tag @s[predicate=<your_namespace>:random_chance] add yay_it_workedI've been using predicates for some things at a basic level, but I haven't found a lot of really good resources that explain everything they can do. This tip is extremely useful, because I didn't know a predicate could just give me a random number.
I really appreciate your help. I am very new to using commands, functions, tags, and predicates. It's useful help like this that helps others succeed here on PMC, and I can't say thank you enough!
{
"condition": "random_chance",
"chance": 0.25
}I really appreciate your help. I am very new to using commands, functions, tags, and predicates. It's useful help like this that helps others succeed here on PMC, and I can't say thank you enough!
For more information about literally everything in Minecraft, especially when looking for a full list of something, you can visit Minecraft Wiki - it's even featured in minecraft.net if you search enough. It's very useful for finding resources for data packs.
