Arqade Asked by natesmileyboy on March 27, 2021
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.
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.
Answered by Svj0hn on March 27, 2021
Min | Max |
---|---|
-2147483648 | 2147483647 |
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.
Answered by MrLemon on March 27, 2021
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:
Answered by BlueWizard on March 27, 2021
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.
Answered by Sentasaur on March 27, 2021
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.
Answered by sentasaur on March 27, 2021
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.
Answered by thatoneguyjim on March 27, 2021
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.
Answered by ExpertCoder14 on March 27, 2021
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 |
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 area_effect_cloud ~ ~ ~ {Tags:["random_uuid"]}
execute store result score @s random run data get entity @e[type=area_effect_cloud,tag=random_uuid,limit=1] UUID[0] 1
scoreboard players operation @s random %= @s range
kill @e[type=area_effect_cloud,tag=random_uuid]
The first command summons an AEC 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 AEC.
Answered by ExpertCoder14 on March 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP