8

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.

8

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
  • You seem to have an unfinished sentence at the end of your second paragraph: 'Using a fill clock, the randomness [...?]' – angussidney Jul 27 '15 at 11:07
  • @AngusAtkinson thanks. That was a leftover from when I planned to use a dummy objective instead of the easier stat version. – MrLemon Jul 27 '15 at 11:09
  • 2
    /scoreboard players set @a[score_RNG_min=9] 0 should be replaced with /scoreboard players set @a[score_RNG_min=9] RNG 0 – BobTheAwesome Sep 5 '15 at 17:07
  • @BobTheAwesome good call. Feel free to suggest an edit for these kind of mistakes. – MrLemon Sep 6 '15 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 Dec 2 '18 at 16:17
3

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:

2

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.

  • 2
    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 Sep 13 '16 at 17:09
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

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

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, privacy policy and cookie policy

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