Entropy Source on Microchip PIC24F

developmentmicrochippicrandom number

I am working to develop an Android accessory hardware. Currently, I am trying to generate an random number (C language) on a embedded system, but the value doesn't change. I am wondering if there is any entropy source that I can use to generator random number.

If there is another way of generating random number other than using an entropy way, I would greatly want to hear it from you!

The software I am using:

MPLAB IDE Version 8.73 MPLAB C Compiler for PIC24 and dsPIC v3.30

The device/tools I am using:

DM240415 – PIC24F Accessory Development Starter Kit for Android

Best Answer

“Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin” \$-\$ John Von Neumann

A good noise source is the breakdown noise of a zener diode. The simple schematic below shows how to get white noise from a zener by cascading two LNAs (Low Noise Amplifiers) to increase the noise level.

white noise generator

If you don't care about the noise being white you can simply use an opamp amplifier with a high amplification, with a comparator following it. The opamp's amplification puts a limit to the bandwidth, and hence the rate of change in your digital signal. If necessary cascade two opamps like the LNAs in the given schematic to get a faster random bit stream.

You can use the SPI module to clock in bytes of random bits from this circuit.
(The SPI is just a simple way to automatically collect 8 random bits, it doesn't add any level of determinism: the input changes continuously and randomly and you never know what it will be at the next clock edge. You can also read an I/O pin and shift that bit's level into your result byte.)

This circuit is a possible alternative solution, also relying on a zener diode as a noise source:

random bit circuit

The schematic mentions the OPA2340 for the amplifier, but isn't clear on the comparator. While the OPA2340 is reasonably fast I would suggest to use a real comparator here as these are usually much faster than opamps. For instance the TL3016 has a propagation delay less than 10ns, and a rise time of 0.5ns typical. This means you can sample random values faster without the risk of coherence between successive samples.

To test the random number generator you can test for normality. This means creating a long string of random numbers, the longer the better. Best thing is to transport it to the PC for the analysis. Count the one-bit sequences, that's the 0s and 1s. There should be about the same number of each. Next repeat for two-bit sequences. There should be as many 00s as 01s, 10s and 11s. Repeat for three-bit sequences, etc.
I'm not a statistician, so there may be better/easier tests. Feel free to add them.


pseudo random number generator