What sources of randomness can be made with commands in Minecraft?
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_min=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.
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.
sorry for this bad visualization. Every Blue box generates 1 or 2 with a 50% chance.
Or you could use the spreadplayers
command 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:
- Minecraft Tutorial: Easy Random Number Generator (CodeCrafted)
- Minecraft Tutorial: Reliable Pseudo Random Output Generator (Cubehamster Innovation in Minecraft)
- Minecraft 1.8 Random number / command generators (Dragnoz)
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.
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.