10

I want to make a mini game in Minecraft were I need a random command generator for 9 commands. I need a random command to give the player a random item.

I can't do the command block in the dispenser any more because it will just drop out instead of being placed.

What different ways are there for generating randomness in commands, and which one would be best for my use?

This question is similar to the one about randomness in Redstone circuits, but I am specifically attempting to get randomness when using commands - the redstone solutions do not apply here.

0

11 Answers 11

13

Running score method: Primitive, simple, but not as good as others

Min Max
-2147483648 2147483647

Details

The simplest solution is not to use an actual random generator at all, because it's not really needed. The randomness can come from a user input instead.

What I mean by that is that you can have a rapidly changing scoreboard objective, and evaluate the score at the moment a button is pressed.

First set up your randomness objective using

scoreboard objectives add RNG dummy

There used to be a stat.playOneMinute which would automatically increase it by 1 every single game tick without another command needed, and it will not be the same for every player (if that is not desired, resetting it for everyone works). However, because updates have removed this statistic, we would need to implement our own commands to increase it by one tick.

1.13+ : scoreboard players add @a[scores={RNG=..9}] RNG 1
1.12- : scoreboard players add @a[score_RNG_max=9] RNG 1

Now create a fill/setblock clock and have it run

1.13+ : scoreboard players set @a[scores={RNG=9..}] RNG 0
1.12- : scoreboard players set @a[score_RNG_min=9] RNG 0

and you're done. To use your random numbers, make one command block for every single outcome and include [score_RNG=X,score_RNG_min=X] with your target selector arguments, where X is the score to use, running from 0 to 8(!). Trigger all of these at the same time. For example

...
/give @a[score_RNG=4,score_RNG_min=4,team=PlayingTheGame] diamond_sword 
/give @a[score_RNG=5,score_RNG_min=5,team=PlayingTheGame] dirt
...

If your command does not use a target selector argument, you can (ab)use execute for that, e.g.

/execute @a[score_RNG=4,score_RNG_min=4] ~ ~ ~ setblock 1 2 3 stone

Nowadays in 1.13+, it is advised not to use this as there are much better alternatives and it requires a running function. This is however the most easy way to generate random numbers.

3
  • 2
    /scoreboard players set @a[score_RNG_min=9] 0 should be replaced with /scoreboard players set @a[score_RNG_min=9] RNG 0 Commented Sep 5, 2015 at 17:07
  • @BobTheAwesome good call. Feel free to suggest an edit for these kind of mistakes.
    – MrLemon
    Commented Sep 6, 2015 at 20:31
  • This creates a unique experience for each player, or if the player isn't involved you can just use @p (as long as someone's online). +1
    – clabe45
    Commented Dec 2, 2018 at 16:17
5

UUID method

This method is the 2nd best method of getting random numbers in Minecraft. The best way is to implement your own LCG.

Min  Max
−2 147 483 648 2 147 483 647

Details

Minecraft's generation of UUIDs to put on entities is one of the most obvious random number generation in the game. If we do this correctly, we can utilize it to our advantage and get a random number based on the UUID.

What we can do is summon an entity with a random UUID and use modulus %= to reduce it within the range we want.

Try these out:

summon marker ~ ~ ~ {Tags:["random_uuid"]}
execute store result score @s random run data get entity @e[type=marker,tag=random_uuid,limit=1] UUID[0] 1
scoreboard players operation @s random %= @s range
kill @e[type=marker,tag=random_uuid]

The first command summons a marker with a tag, the second one stores the first value of its UUID in a score, the third one does the math to reduce it within the range we want, and the fourth one kills the marker.

1
4

There are many ways of creating a great working random number generator using randomness-Algorithms Mojang already build in.

For example a Dispenser chooses a random item to eject when being triggered. You could detect the stackability of the item using a comparator - A item which is stackable (i. e. Cobblestone) only outputs a signal of length 1 while a item which is not stackable (i. e. Doors) will output a singal of length 2.
Thus you get randomly the number 1 and 2. You could use multiple of them in an array to generate higher numbers. You can adjust the probability of a specific outcome by manipulating the amounts of items inside the dispenser/dropper.

image
sorry for this bad visualization. Every Blue box generates 1 or 2 with a 50% chance.

Or you could use the spreadplayerscommand which also uses java build-in random number generator. Spawn an Entity such as an sheep and then spread it. It will land on an random position. This position can be logged by pressure plates but will require more space.

There are multiple videos on YouTube about this. Maybe you want to watch one of them:

4

Predicate method: A special source of random in Minecraft

If you want to have a single command to run on a certain chance or not, and want to use one of Minecraft's built-in randomization methods, try out predicates. Predicates are special condition files that are queried with an entity you select to see if the conditions of that entity match the predicate.

Luckily for our use, the predicates have a random_chance condition, which means that the predicate will return a true condition with a customizable level of chance.

Here is what your predicate file will look like:

{
  "condition": "minecraft:random_chance",
  "chance": 0.1
}

chance is a number you can customize from 0.0 to 1.0. The number denotes the chance of the condition to pass. 0.0 means it will never pass, and 1.0 means it will always pass.

You can query the predicate like this:

execute as @r if entity @s[predicate=<predicateName>] run ...

Creating different predicates and making a binary tree of them with different weights will allow you to make limitless possibilities for chances and randomness. However, it may not be worth it if you have too many possible outputs, and you may want to try a different way.

4

My email got deleted and I can't access my original account. Ignore the other sentasaur post. I updated the random number generator so it's even easier to set up. First, place a layer of downwards-pointing "impulse,unconditional,needs redstone" command blocks filled with the command:

/setblock ~ ~2 ~ air

Then use chain command blocks pointing downwards, with the commands you need to run for each random output you plan to set up.

For the selector, place a platform of stone blocks directly on top of the top command block layer, then use armorstands with a specific tag (ex.RandomNumberGenerator) on the very top, one for each stone block. Use the command

/execute @r[type=ArmorStand,tag=RandomNumberGenerator] ~ ~ ~ setblock ~ ~ ~ redstone_block

to activate the randomness!

Note: If you're playing in Minecraft 1.11 snapshots, the second command is actually:

/execute @r[type=armor_stand,tag=WhateverTagYouChooseToUse] ~ ~ ~ setblock ~ ~ ~ redstone_block

due to name changes for armorstands.

1
  • 3
    You should edit the other post rather than posting an entirely new answer. Other users may not see this post, since the other post has been upvoted.
    – Vemonus
    Commented Sep 13, 2016 at 17:09
2

I've come up with a pretty easy random number generator that is also pretty compact. Here it is: first, you build a platform with as many blocks on it as random outputs that you need. while standing on each block of the platform, use this command:

/summon ArmorStand ~ ~ ~ {Tags:["R"]}

this will summon a pretty normal looking armorstand, but the important thing is the tag R (or whatever you type in- it must stay the same throughout the procedure) Place 1 armorstand for each block [that is, output], then directly underneath the platform, with a gap 1 block high, place another platform, with weighted pressure plates in the gap, 1 for each armorstand. On the underside of this platform, place a chain of command blocks travelling downwards, 1 for each pressure plate, with the first one being an unconditional impulse block with the command:

/kill @e[type=Item,r=2]

this will instantly remove the item that triggers the pressure plate. Underneath this command block, you can now place a command block chain to do whatever you want for that output. You can see where I'm going- now you can build a redstone circuit for your desired output speed, triggering the command block

/execute @r[type=ArmorStand,tag=R] ~ ~-2 ~ summon Item

This summons a dropped item (stone by default) two blocks below a random armorstand, triggering the weighted pressure plate directly below the armorstand, thereby setting off the chain of command blocks directly beneath that.

Note: it's important to place the weighted pressure plates on a layer of stone/other material, and not directly on the command blocks. That's because the block directly underneath the pressure plate becomes "powered", and activates all adjacent blocks as well. If the powered block and the activated blocks are all command blocks, this could set off up to 5 outputs simultaneously! (unless, of course, that's what you want)

My favorite part of this random output generator is that you can make it as big or as small as you want. I've made generators with 2 outputs, and I've made ones with 400.

Credit to Lorgon111 for teaching me the /execute command.

0
2

PRNG/LCG Method

Post based on this reddit post

Range varies depending on the implementation. The one in this post has the following range

Min Max
−2 147 483 648 2 147 483 647

Creating your own pseudo random number generator is in (almost) all cases the least lag intensive and most flexible way of getting a random number. Using functions and this method you can get hundreds of different random numbers per tick without causing any significant lag, as it's just a handful of scoreboard commands.

What is a PRNG/LCG?

A PRNG is a pseudo random number generator, pseudo because it's not really random and actually predictable, but to a human it looks random enough. Most computer based random generators are actually PRNGs, because computers aren't very good at actual randomness. They were designed not to. Wikipedia on PRNGs

LCG stands for linear congruential generator and it's one of the most common ways to implement a PRNG, probably because of it's ease of use, reliability and the fact that it has been known for a long time. It's just a bunch of arithmetic operations chained together. Wikipedia on LCG

LCG in Minecraft

Setup

Our only option for mathematical operations is scoreboards, so we'll be using those. For the LCG to work, we'll need a few hard-coded values and for ease of understanding we'll use 2 different dummy scoreboards.

/scoreboard objectives add RNG_Constant dummy
/scoreboard objectives add RNG_Variable dummy

Then we set the constants that we'll work with, as well as initializing the seed.

/scoreboard players set C_1000 RNG_Constant 1000
/scoreboard players set C_314159 RNG_Constant 314159
/scoreboard players set C_2718281 RNG_Constant 2718281

/scoreboard players set RNGseed RNG_Variable 0

Getting a random number

Finally, the list of commands to run in order to get the random number. To make it slightly less predictable, we change the starting value of the variable to something like the in-game time or the amount of entities or any other variable amount using execute store (line 2).

/scoreboard players add RNGseed RNG_Variable 1
/execute store result score RNG RNG_Variable run time query gametime
/scoreboard players operation RNG RNG_Variable *= RNGseed RNG_Variable
/scoreboard players operation RNG RNG_Variable *= C_314159 RNG_Constant
/scoreboard players operation RNG RNG_Variable *= C_2718281 RNG_Constant
/scoreboard players operation RNG RNG_Variable *= RNG RNG_Variable
/scoreboard players operation RNG RNG_Variable /= C_1000 RNG_Constant
/scoreboard players operation RNG RNG_Variable *= C_314159 RNG_Constant
/scoreboard players operation RNG RNG_Variable *= C_2718281 RNG_Constant

After running these commands, the RNG RNG_Variable score will be a random number that you can use in whatever way you see fit. /scoreboard players get RNG RNG_Variable.

You can scale the result down to a desired range using scoreboard players operation with the modulo (%=) operator. Note that minecrafts modulo algorithm will only result in positive numbers, so keep that in mind if you need negative values, too.

1

Depending on how often it has to be used, this method posted on reddit may be a good one.

It basically revolves around having a scoreboard objective being set to all your random numbers (1-9 in your case, probably) by a sequential signal (so repeaters in a circle or a minecart going around).

As long as you don't poll the value too often, it should appear pretty random. There are also some ideas in the post on how to improve it.

1

Determining a random Boolean value (true/false) is trivial (for example, with a random chance predicate). This can be extended to generate random values from a larger range in a bitwise manner.

For example, to generate a random integer from 0 to 7, one could use three bits (representing each with a scoreboard objective). For each bit, randomly determine whether the bit is true or false using your trivial Boolean method: if true on the first check, bit1 gets assigned a value of 1; if true the second time, bit2 gets a value of 2; if true the third time, bit3 gets a value of 4. The next bits in a larger number would get values of 8, 16, 32, and so forth. Your random value would be equal to a fourth objective that gets assigned a value equal to the sum of all of the bits. Standard bitwise math.

If the predicate example is used and the odds are set to 50% for true/false, then one should get as even a distribution as Minecraft can provide.

1
1

In Minecraft 1.20.2 and later versions, you can use the built-in /random command to produce random numbers.

0

The way that I did it was simple, I got 6 armor stands (my generator is 1-6) and used:

/testfor @e[name=1]

and reversed the signal. So then every time an armor stand was destroyed, it would respond and run a command. So then I just used:

/kill @r[type=Armor_Stand]

and boom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.