How can I make a Random Number Generator with vanilla redstone? [duplicate]
I'm working on a game for my Minecraft server that includes the element of chance.
The idea is that there are 6 buttons. One of them (randomly determined by redstone circuitry) results in wealth, prizes, etc. The other five result in doom.
When you enter the room the doors close initiating the game and beginning the primary circuit which selects which of the 6 buttons is the winning button. How to actually make the circuitry do this is my problem! I've got the door controls, win/lose circuits, and the reset circuit to prepare the room for the next player all worked out, but I'm stumped on how to make redstone do something random.
How can I use redstone to choose one of the 6 buttons to be active, randomly?
Solution 1:
One of the most common ways to make a real random circuit is to create a very fast clock that quickly cycles through the possible outputs, which will stop as soon as a person pushes a button. If the circuit is fast enough, or hidden from them, then the exact moment they stop the circuit is sufficiently random to serve many applications.
This can be implemented in redstone using a 6-state ring counter that is constantly "ticked" by a clock circuit, with the clock stopped by the latched human input (which in this case, would be the same as your door-open button), so that when they push the button the ring counter stops changing and its output becomes your button-selection data.
Even better, but more complicated, would be to create a memory cell that "captures" the state of the ring counter when the human input button is pushed, and uses the memory cell as the activation data instead, so that the ring counter never has to be stopped from cycling. That would make the ring counter even less predictable, and make the randomness higher quality.
Solution 2:
Random circuits can be made by putting different items into a dispenser and launching one out. After you determine which one is dispensed, you can reload it into the dispenser.
In your case, since you want to have six random outputs, you could put six unique items into a dropper or dispenser (such as six different shades of colored wool). When the player presses a button, the dropper will randomly throw out one of the items.
Then, you can use flowing water to carry the item over six hoppers. Each hopper should be completely full of the item that you want to detect, minus one. (So for example, have a hopper with 64-64-64-64-63 blue wool, another with red wool, etc.) You can detect which hopper collects the wool by using a comparator, which only outputs on maximum strength when a container is completely full1.
Once you know which hopper collected the item, you can use this random output for your desired purposes. The only cleanup necessary is to move that item from the hopper back into the dropper so that this system will be reusable. You could have another set of hoppers underneath the first set and temporarily enable them for one tick when the comparator is at full strength, so that they pull the item out again and put it back into the original dropper.
Solution 3:
Have 2 sticky pistons "fighting" over a redstone block. 2 pistons facing each other with 3 blocks in between them, and a redstone block in the middle of the open space.power the pistons with a pulse at the same time and once the pulse ends, the pistons retract and there is a 1/2 chance that either piston gets the block. Put the output in between the redstone block and the pistons and if the left piston pulls the block towards it, the output will be left side.
Solution 4:
You can also use pig spawners to make it random. You could put a pressure plate under each spawner and, whenever a pig hits a pressure plate, that would be the button that is good while the rest would be bad until another pig spawns and hits another pressure plate.
If you don't know how to get pig spawners, since you can't even obtain them in creative mode, all you have to do is use cheats to spawn them in.
If you dont know the ID number of pig spawners, just go to this website: Minecraft ID List. Oh, and by the the way, you will have to put grass next to the spawners since pigs can only spawn on grass.