Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate random numbers in a microcontroller efficiently?

How do I generate random numbers in a microcontroller efficiently? Are there any general guidelines or a particular fast method?

like image 753
Donotalo Avatar asked Oct 13 '09 05:10

Donotalo


People also ask

What is the best method to generate true random numbers?

To generate “true” random numbers, random number generators gather “entropy,” or seemingly random data from the physical world around them. For random numbers that don't really need to be random, they may just use an algorithm and a seed value.

Which module is used to generate random numbers?

Python Random module is an in-built module of Python which is used to generate random numbers. These are pseudo-random numbers means these are not truly random. This module can be used to perform random actions such as generating random numbers, print random a value for a list or string, etc.


2 Answers

One normally generates pseudo-random numbers and not actual random numbers, although both are possible at varying rates.

There are two general categories, depending on whether the sequence will be used for cryptographic purposes. The primary distinction is whether knowledge of one number in the sequence allows prediction of the next. General purpose RNG's don't worry about whether knowledge of the algorithm would allow an observer to duplicate the sequence, and they run quite a bit faster.

A typical general purpose RNG algorithm is the Mersenne Twister. There are many public implementations of various algorithms. See here for one.

If the MT requires too much memory, a half-way-decent fallback is the linear congruential generator. (The MT wasn't invented until 1997.) This generator has certain issues but it requires almost no memory, almost no code, and is extremely fast. Implementations are everywhere, and it was covered in detail in Knuth's Seminumerical Algorithms.

To seed any RNG, you will need a source of entropy, see http://en.wikipedia.org/wiki/Entropy_(computing) (Note: SO gets confused about the ()'s in that link.) This is typically derived by timing events that the CPU can observe, such as keystrokes, (I guess that won't work for you) interrupts, and packet arrivals. A real time clock is often an acceptable source if it maintains its own state, as reboots are rarely timed in any sort of sequence.

like image 115
DigitalRoss Avatar answered Sep 17 '22 22:09

DigitalRoss


you can store a seed to EEPROM, and when device boots, you can increment a seed and store it again. So, every reboot youll have different random number.

like image 23
Igor Avatar answered Sep 19 '22 22:09

Igor