Button to extract a fair random bit from analog noise - Page 1
Forum section: electronics / projects

author: RoGeorge
author_rank: Super Contributor
timestamp: September 21, 2024, 09:23:27 am
content: Seen mentioned that the typical Zener diode will generate plenty of noise when reversed polarized at low current. This property can be used to make a typical generator of analog noise:https://www.nutsvolts.com/magazine/article/bipolar_transistor_cookbook_part_5I would like to use the randomness of the analog noise to light a "red or green" LED at each press of a button. One way would be to feed the analog noise to a Schmitt trigger.https://www.nutsvolts.com/magazine/article/bipolar_transistor_cookbook_part_1On positive spikes in the noise, the output will flip one way, at the negative spikes, the output will flip the other way. As long as there is noise at the input of the Schmitt trigger, the output will keep flipping between red and green.If we add a button between the two stages, to disconnect the noise source, when we press the button the Schmitt trigger will remain at its last value. That state will remain displayed as a red or a green LED as long as the button is kept pressed. The problem is, how to be sure the distribution of red/green is fair, with guaranteed 50% chances?For digital randomness, there is the Von Neumann's debiasing algorithm, which will produce fair 50% chances guaranteed, no matter how unfair may be the distribution of 0/1 in the incoming digital noise. However, this is done by taking pairs of consecutive samples, and rejecting those pairs that are of the same value, so it is done at the expense of sometimes throwing away some of the incoming samples.To implement this for the analog noise, would mean some of the button presses won't generate any bit at all, and will have to press again the button, one or more times, until I get a valid red/green choice with guaranteed equal distribution. Would be annoying to press the button many (unknown) times.The must have- aguaranteed50/50 chances for red/green- ateachpress of the button- fromanalognoisePreferably would be to keep it all analog. Eventually the circuit can have a microcontroller, but the source of randomness must remain the analog noise, no pseudo-random generators and no internal random number generatorsIn less words, a button to simulate the flip of a coin based on the input analog noise. How to make that with a guaranteed 50/50 distribution of red/green?

author: Whales
author_rank: Super Contributor
timestamp: September 21, 2024, 10:03:40 am
content: 50% ratio is hard. Perhaps you could use a comparator to compare the random noise with an averaged (RC filtered) version of the noise.Note: Your circuit doesn't latch, so whilst your are pressing the button it will continuously output random results (fast enough to make both LEDs look lit). You might want a D flipflop that is edge-triggered by the buttonpress.An alternative circuit to consider might be a digital LFSR.

author: Whales
author_rank: Super Contributor
timestamp: September 21, 2024, 10:05:04 am
content: Oh and if you're struggling to get good noise out of the zener: you might need to try a few as they all have different noise levels. I think I needed a few transistors worth of gain to get it to work. You can also use the BE junction of an NPN transistor biased backwards (it breaks down vaguely around 8-12V IIRC).

author: RoGeorge
author_rank: Super Contributor
timestamp: September 21, 2024, 10:44:31 am
content: Indeed the schematic is only a draft. Can be eventually made to latch, just that I'm not sure how to make that precisely 50/50 at each button press, like in the digital de-biasing algorithm of Von Newmanhttps://mathoverflow.net/questions/152107/proof-of-von-neumanns-debiasing-algorithm

author: tggzzz
author_rank: Super Contributor
timestamp: September 21, 2024, 11:14:19 am
content: Quote from: RoGeorge on September 21, 2024, 09:23:27 amSimplest solution is to have an oscillator driving a flip flop. The output of that is guaranteed to be 50:50.Now you want successive samples to be random, i.e. uncorrelated with each other. If the clock frequency is high enough (e.g. MHz), a human pressing a noisy/bouncy button will generate a sampling signal that is uncorrelated with the clock and where there is no coherence between successive samples many clock cycles apart. Job done.

author: forrestc
author_rank: Unknown
timestamp: September 21, 2024, 11:40:30 am
content: Quote from: tggzzz on September 21, 2024, 11:14:19 amI was about to say essentially the same thing. Stick a flip/flop on the end of a crystal oscillator with a button interrupting a signal. Probably could be as simple as a Clock oscillator in the Mhz range driving a pair of 7400 gates with a led off of each output. Or, use some of the RC oscillator circuits which use a set of inverter gates to do the same thing.If I was going to go more analog, I'd be curious if running a pair of transistors in a flip/flop arrangement (like you would do with an alternating LED circuit) with very small R's and C's would run fast enough to do this. This, though, would not necessarily be 50/50 as most, if not all, of those circuits have a separate R/C pair on each side so how close you would get to 50/50 would depend on how well matched the two halves are.In both cases, your source of randomness is the combination of button press time and button bounce noise. You're effectively building a high precision timer, and then throwing away all but the final bit of the time result, which with a fast enough clock is going to be random.

author: wraper
author_rank: Unknown
timestamp: September 21, 2024, 11:56:16 am
content: QuoteQuoteIt will be worse than just using square wave generator (+ divider remove any bias within oscillator cycle), true 50/50 randomness is ensured by your finger pressing the button to begin with. By adding analog stuff you only add bias to the result.

author: Siwastaja
author_rank: Super Contributor
timestamp: September 21, 2024, 12:08:59 pm
content: Quote from: wraper on September 21, 2024, 11:56:16 amThis. Just use square wave with significantly higher frequency any human being is able to track in their head. So e.g. something at least 100 Hz or so. Then just store the current value into flip flop when detecting button state change - simplest would be a D flipflop and a button driving the clock input (debounced first).

author: Psi
author_rank: Super Contributor
timestamp: September 21, 2024, 12:22:07 pm
content: How many samples do you need this thing to generate while remaining random?You can get some pretty good randomness by storing in eeprom/flash a large chunk of true random numbers obtained from a true random number generator.Then using a pseudo-random number generator not for your output data but to pick how many index positions in the above random data to jump each time you need a new number.Is this approach perfect, no, but it's pretty good. Depending on how many random numbers you need out of the thing in its lifespan

author: tooki
author_rank: Super Contributor
timestamp: September 21, 2024, 12:49:39 pm
content: Quote from: tggzzz on September 21, 2024, 11:14:19 amThis is exactly what I was thinking, too.Everyone knows those little “electronic dice” kits. Those work the same way: an oscillator that runs while you hold the button and stops when you let go. (In the dice, the clock just runs a binary counter instead of a flip-flop.)I don’t see how anything could improve on the randomness caused by the human pressing the button and the button’s switch bounce.

author: MK14
author_rank: Super Contributor
timestamp: September 21, 2024, 01:50:45 pm
content: Trying to be totally minimalist and 'cheap'.A British (actual copper) 2p (£0.02) polished (for electrical conductivity), a rather small box, just big enough.A single LED (e.g. Red) plus suitable current limiting resistor, with contacts, or bits of old PCB, or copper foil etc.Carefully cover one side of the coin (2p) with a very thin film of plastic/sellotape/etc.Invert the box (180 degrees) for off/unused.To do the random selection, invert the box (180 degrees), it will then randomly (coin toss), either short the contacts, and light the 'win' or 'your turn' light, or not, largely randomly (except for slight weight imperfections, because of plastic tape, etc).Shake while inverting, for extra fun and to ensure better randomness.

author: RoGeorge
author_rank: Super Contributor
timestamp: September 22, 2024, 08:30:57 am
content: Thank you all for the ideas. Will keep the diode as a noise source, then add a D-type flip-flop at the end, to divide by 2 and make red/green chances equal.Meanwhile I've just used the last digit from a stop chronometer.(the 1/100 of a second digit from a 2G Nokia phone that usually sits on the bench anyway, as a clock/timer/infrared remotes checker/audio notes/etc.)