I want to do random command from a list to run but in datapack and without taking space from a world also for a tick

2 Answers 2



My solution uses an nbt array, grabs a random number, takes the value at index that was generated, and runs the command in a command block.

Preparation in Minecraft

So in preparation, you will need an array in a storage which we’ll call rcg with each command as a value. Make sure the commands are in strings. You can do that with the following command.

data modify storage rcg commands set value [<values>]

If you ever want to add another command, the process is simple. All you need to do is append the command. The process is what follows.

data modify storage rcg commands append value “<command>”

Now, we need a Impulse Command Block set to Needs Redstone to run the command. We’ll put the command block at coordinate 0 0 0.

Preparation Outside Minecraft

Now, we need a random number generator. Cloud Wolf has a random number generator that can use a range. The rng comes with trig as well although we won’t need trig for this. The download link is here. Now you need to install the datapack into your world and then you are ready to go.

Core Commands

Now, we get to the commands. So in a new function, we need a command to retrieve the length of the command array and store it in the range max scoreboard value. We’ll call this function main. You can do that with the following command.

execute store result score in1 math run data get storage rcg commands

Now, we run the function for the rng.

function math:rng/range

Next, we set a new nbt array to the original one to iterate through the array later.

data modify storage rcg itercommands set from storage rcg commands

Now, we need a new function to remove the first value from the itercommands array, remove 1 from the generated number, then run the function again unless the out value is 0. We’ll name this function findatindex The following commands does this.

data remove storage rcg itercommands[0]
scoreboard players remove out math 1
execute unless score out math matches 0 run function <namespace>:findatindex

Now, we run this new function unless out is 0.

execute unless score out math matches 0 run function <namespace>:findatindex

Next, we take the first value in the itercommands array and store it in the command block’s command.

data modify block 0 0 0 Command set from storage rcg itercommands[0]

Next, we power the command block by using one of multiple methods. 1: We modify the command block to set always active on. 2: We modify the command block to set powered to true. 3: We set a redstone block next to it. I will go with the 3rd.

setblock 0 0 -1 redstone_block

Finally, we schedule another function for 1 tick in the future, to remove the redstone block. We’ll call it rcg_end.

schedule function <namespace>:rcg_end

There you go. You are finished. Please let me know if you have any bugs or questions.

  • There is a way less complicated at least without /data because I so confused from this I guess you've been working this alot but there is a simpler way?
    – panda
    Commented Dec 12, 2020 at 9:24
  • What is the source of randomness that the data pack provides?
    – One 2 Many
    Commented Dec 20, 2020 at 17:46
  • @ExpertCoder14 the datapack uses lcg to generate numbers. Commented Dec 20, 2020 at 18:39

I found something similar but more easier for who that want to do

the generator


what I do after is
(in main function) for running command:

/execute if score @s RANDOM matches <any number in range that not used>  run function <namespace>:<function>  

(in the end of the function)for not repeated commands:

/scoreboard players set @s RANDOM 0
  • Your method may be better for simplicity, but mine is better for having a bigger range of outcome. My way is for automation, and there is no hard-coding needed. If this works for you then great. This just might not work for others. Commented Dec 12, 2020 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .