How can I make a random number generator with command blocks in Minecraft?

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.

  • How to set Command Block(s) to test for a particular time and output a message?
  • What's the minimum amount of stuff you need from the surface to be self-sufficient underground?
  • How can I improve the performance of Minecraft?
  • What is the smallest iron golem spawner?
  • Run MineOS from USB Stick
  • Where are my Minecraft saves located?
  • Armor not providing armor bars/protection
  • How to make villagers spread around
  • How do I create a very long delay with Redstone?
  • What is the “Use VBOs” setting?
  • Minecraft Failed To Bind Port
  • Why won't my nether portal bring me back where I left?
  • 5 Solutions collect form web for “How can I make a random number generator with command blocks in Minecraft?”

    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 stat.playOneMinute
    

    Using stat.playOneMinute will 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).

    Now create a fill/setblock clock and have it run

    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
    

    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:

    • 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.

    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.

    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.

    We love Playing Games, especially Video Games.