Given 5 wires (possibly more), I want to press a Reset button, and have exactly one of those wires, randomly selected, become active - and remain that way (until the next button press). What is a simple way of doing that?
This Q/A is actually related to another Question, my Answer to which, was sufficiently complex (and had so many diagrams), that I decided it was worth a separate post.
Now, there are a lot of random-number generators out there, some very clever (one uses a 13-bit linear feedback shift register - wow). But I wanted something simple, extendable, and easily-understood. There are probably better ones out there :) But I think this one works well enough to be used as part of the circuit in the other Question.
It's basically a 5-clock (using the new repeaters - I love those), connected to some memory flip-flops, with a reset button.
Top-level circuit diagram
5-clock circuit diagram
The arrows are repeaters, set to either 0.3 or 0.2 seconds delay (or none if blank). The 0.2 is to compensate for the length of wire before it, which adds some delay. The clock's entire cycle time is about 1.5 seconds.
333 circuit diagram
How to use it
Clear the 5-clock (repeaters get frozen), on server reboot/game reload. This can usually be done by dropping/removing a torch by the circuit, and/or removing and replacing a section of wire. When all of the repeaters are unpowered, the circuit is clear.
Inject a pulse into the 5-clock. Press the button on the Auto-Pulse Injector, and it will start the clock.
Press the Reset button - exactly one of the five wires, will now become active. If any wires were previously active, they turn off.
The clock cycles through 5 torches in about 1.5 seconds, and when you press the Reset button, whichever wire is next to an active torch, will be locked into an active state.
At this point, someone is no doubt complaining, "you call that random"??? :) Why, yes, I do - unless you walk to the button at deterministic time intervals, it's very random. Some people have designed RNGs that use mobs walking around - I'm using the player. :)
However, if you want to add more randomness to it, use the T.B.D. circuit - basically, that means that you: go to the button, look at the Seconds digit on your wristwatch, and when it hits zero, press the button. This will give a range of 1-10 seconds, averaging 5, in which the clock will cycle, resulting in an unpredictable wire activated.
I realize there are other ways of getting a random event. In fact, I had originally created a rapid-pulsar that burned out, then relit in about 5-15 seconds. I was working on having the relight event trigger a JK flip-flop, sending a pulse to the clock. But it was a) trickier than I thought, and I wanted to get an answer posted :) and b) somewhat redundant. All I was trying to do was wait for a random interval before picking a clock torch - and it's easy enough for the player to just look at his watch for a random interval. :)
Sometimes, the best solution, doesn't require a lot of complex machinery. :)
Also, this design is for a specific application - in the original Question, the person wanted a reset button that changed the wire, which means they were actively choosing when to get a random number. If your application involves disco lights flashing at random, then no, this wouldn't work for that. But there's already plenty of solutions for flashing lights randomly.
The 333 circuit is fairly mundane, I claim no originality - the only reason I gave it a name, is I had to give it some label in the diagram for clarity. :)
Update: Although I originally only used a manually-activated Pulse Injector, it required that the player flip the switch on/off quickly. Just for fun, I decided to add an Auto-Pulse Injector to make it simpler to activate the clock. (The design for which came from the Wiki).
With the 1.5 update with hoppers and comparators you can make a dispenser face upwards in to a downwards facing hopper and a comparator on the hopper (by the side of it), then place any item that can stack and a item that cant stack in the dispenser, then power the dispenser, a random item will go in to the hopper. If it's a non stacking item it will give out a signal strength of 2 if it's a stackable item it will give out a signal of 1.