21
\$\begingroup\$

I just read through question AVR Random Number Generator and encountered a number of ways to generate random seeds in an AVR:

  • Use a special-purpose "Secure AVR"
  • Use an internal temperature sensor
  • Read unwritten EEPROM
  • Measure time intervals between user input
  • Use a floating ADC pin.

Why not just a single digital pin, configured as input without pull-up and floating? In theory that should generate a stream of random bits. Why not use this? Is the state too slowly changing? Does it tend to stuck at 1 or 0? Any other problems?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Secure AVR is dead AFAIK \$\endgroup\$
    – vicatcu
    Dec 3, 2012 at 16:44
  • 1
    \$\begingroup\$ Isn't unwritten eeprom always 0xFF ? \$\endgroup\$
    – vsz
    Dec 4, 2012 at 7:12
  • \$\begingroup\$ @vsz, maybe if you write something to it and forget what you wrote, it's "random" now. lol. \$\endgroup\$
    – Vorac
    Dec 4, 2012 at 7:20

7 Answers 7

5
\$\begingroup\$

"Random" is a tricky word. In some contexts, it simply means "unpredictable", but in other contexts — in particular, the ones associated with signal processing and cryptography — it means "statistically uncorrelated".

Even if the value read from a floating pin is unpredictable ("random" in the first sense), it is not likely to be useful in the second sense.

\$\endgroup\$
16
\$\begingroup\$

The self-evident difference between using a digital input, and an analog one, is that there's a greater span of seed values possible in analog.

Second, but perhaps more crucial: If the digital pin's floating "median" value is not precisely midway between logic sense levels (which are not the power and ground rail levels, but switching thresholds for the input circuitry) the resultant bit stream will have a strong bias towards 1 or 0. This bias skews seeds much more strongly than a skew in analog levels would.

It would take very little coupling, either from nearby traces or signals, or through some resistive pull-up / pull-down, whether designed or accidental, to push a "floating" digital pin to unfloat - and that could as well happen post-deployment. In engineering any design, this form of failure mode is best avoided.

\$\endgroup\$
16
\$\begingroup\$

A floating input is undefined. It could give you random bits, but it could also tend to get stuck at a 0 or 1. Most of the time it will get stuck. And when it is truly floating, it could cause increased system noise and power consumption. Floating inputs are generally a bad thing, even when the inputs are not being used for anything.

\$\endgroup\$
1
  • \$\begingroup\$ Not only will it get stuck at 0 or 1 some of the time, but unless the two transistors that make up the first stage of the input buffer are PERFECTLY balanced, or there is some external current source on way or another, the input will float up to a 1 or down to a 0 eventually. \$\endgroup\$
    – wjl
    Sep 5, 2015 at 6:10
10
\$\begingroup\$

The simplest solution is to build a hardware random signal generator circuit out of a misused open-collector transistor circuit. Use the bit-stream output to an input on the micro-controller. Sample the incoming bits at intervals. To ensure a roughly even number of 1s and 0s, simply use the change of state as a 1 and no change as a zero. Google for transistor noise generator for more information.

\$\endgroup\$
3
  • \$\begingroup\$ Interesting. What does a "misused open-collector transistor circuit" look like? \$\endgroup\$ Jan 2, 2013 at 13:34
  • 7
    \$\begingroup\$ @Remiel - You reverse-bias the base-emitter junction of a transistor, and it acts like a really crappy zener. You take the very noisy zener, and you amplify the noise to get get random bitstream. See robseward.com/misc/RNG2 \$\endgroup\$ Jan 2, 2013 at 14:29
  • \$\begingroup\$ @ConnorWolf Probably worth seeing Are reverse biased transistors stable too. That explains why Rob had that drift problem with his design. "Crappy Zener" is exactly the correct description... \$\endgroup\$
    – Paul Uszak
    May 8, 2021 at 12:43
3
\$\begingroup\$

One random generator that is available in many microcontrollers is the error between two clock sources (perhaps one internal and another external). When you compare them, they drift slightly due to noise and that can be used.

In the end it depends what you need it for? A little randomization and you can even use an ADC pin connected to nothing. Or just use an algorithm like

Mersenne twister http://en.wikipedia.org/wiki/Mersenne_twister

None if this is cryptographically secure though, but good enough for most purposes.

\$\endgroup\$
2
  • \$\begingroup\$ that is the approach taken in the msp430 app note in the referenced prior question, to my knowledge AVR clock system doesn't support this technique \$\endgroup\$
    – vicatcu
    Dec 3, 2012 at 16:46
  • \$\begingroup\$ Correct. It should still be possible to do it with the same clock source, even if it's not directly supported. \$\endgroup\$ Dec 3, 2012 at 17:04
3
\$\begingroup\$

The quality of a random seed and its algorithm is determined by the amount of entropy or uncertainty in each random bit. Then the best generator of a seed uses high entropy with very few steps rather than many steps of low entropy.

A good example of high entropy uses the Latin Squares method for a random seed.

The floating input is easily biased by flaws, leakage or ingress noise.

\$\endgroup\$
0
\$\begingroup\$

In a PIC18F microprocessor you need to have a seed for the random number. The seed can be anything from 0 to 32767 (15 bits). I would suggest at the start of the program you make one of the pins and analogue input pin. Read this pin and take the least significant bit and put it into a 16 bit variable. Then shift the variable to the left by 1. Read the pin again and put the LSB of the ADC result into the first bit of the variable you stored the last bit in. Shift left again.....

Repeat this until you have 15 bits in your variable. This is the number you need to put into your seed.

After you have done this, you could simply make the analogue input pin a digital input pin and enable a weak pull up on it, or make it a digital output pin. Either one will stop this pin from floating.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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