5

I want the most cost effective way for a basic pseudorandom number generator in a microcontroller. I will use the PIC16F54 which has extremely limited resources. I am thinking about using noise with simple analogue circuitry to generate random 1's and 0's at one of its digital inputs. The degree of randomness is not critical. However, the level of EMI noise that the circuit will be subjected to is unknown, besides the fact that it will be in a residential setting with little or no shielding.

Are there any particularly simple low-cost circuits that could be used? Or should I simply focus on some basic algorithms? (There will be one random seed generator: a push switch, after which the algorithm could do its thing. I should probably ask on Stack Overflow for some simple algorithms).

davidcary
  • 17,426
  • 11
  • 66
  • 115
Jodes
  • 5,044
  • 9
  • 48
  • 75
  • The simplest solution (without any analogue) is to start a timer running and then when the user presses the button read the timer value. No human can repetable perform the same action to the single ms I believe. If you don't have a timer available then it might be more difficult depending on your resources. – Spoon Aug 02 '13 at 12:15
  • How limitted is limitted? I'm pretty sure that typical software-only psuedo-random number generators only need to store a single word of state. I would think that anything that requires reading an ADC or even a digital IO pin would need more reasources than that. The time of the button press might be a good solution for seeding the software PRNG. – The Photon Aug 02 '13 at 15:39
  • Better to say, there are software-only PRNG's available that need only a couple of words of storage --- looking a bit on Wikipedia it seems that the higher quality ones might store hundreds of words of state. – The Photon Aug 02 '13 at 15:49

3 Answers3

3

Given that the degree of randomness is stated as not critical:

One random number generation mode that takes minimal resources is to read an open ADC pin within the code, and use the difference between one reading and its previous as a random number source. Make sure the ADC pin is not pulled high or low.

Using the differences eliminates any specific bias on the ADC pin, and provides a random sequence of zero-biased values. Depending on bit depth required, add up a series of such values if necessary.

The method lends itself to easy experimentation, so you can know pretty quickly whether the approach is providing sufficient entropy to meet requirements. If greater entropy is needed, connect an open wire to the ADC pin to provide greater sensitivity to external electric fields.

Anindo Ghosh
  • 50,188
  • 8
  • 103
  • 200
3

You ask for an analog solution, but digital uses even less resources!

I'd recommend implementing a linear feedback shift register on the microcontroller to generate a psuedorandom bit sequence, and break the sequence up into appropriate chunks. This will take a few bytes of memory, and some shift and logical operations.

The math is well understood. You need to select your feedback bits carefully, and the more bits you spare for this purpose, the longer your pseudo random sequence will be.

THIS LINK has the good feedback bits for up to 33 bits.

Scott Seidman
  • 29,274
  • 4
  • 44
  • 109
2

Transistors can generate some very "good" noise.

https://mywebspace.wisc.edu/lnmaurer/web/minirng/minirng.html

This is a fine example but the idea is that if you don't fully bias a transistor, it will output noise. I haven't tried his particular circuit but I don't see any problems with it.

Noise diodes are actually the best for these types of noise sources but are trickier to screen and probably overkill for your application.

scld
  • 2,216
  • 15
  • 17