Fast random number?

sermad

Member
Hi all.

I've searched the forum and people have linked to Entropy for random numbers. I've tried it, and it works just fine. The main drawback is that it is insanely slow (for my needs).

Any recommends for something that works to an OK degree and can execute quickly?

thanks.
s
 
Last edited:
Multiply analogRead() of an unconnected analog pin by analogRead() of another unconnected analog pin, scale down from 0..1046529 to what you need.

Not the best random number sequence, but it runs quickly.

[edit - I looked up this technique and jp3141 may be right; one technique to introduce more randomness seems to be doing some math with the above number and micros(), e.g. mod one by the other or feed both through a hash generator]
 
Last edited:
Thanks all.

I've had a tiny bit of success with analogRead before but had a bit of jitter on there. Right now I'm pre-computing the random numbers into an array and then just cycling through it. An old school lookup table. Clunky but it works for me.
 
A table generated off-line from http://random.org or similar is a great idea! No point in messing about with random number generators that are known to be biased or need to be vetted if you can get by with a table from a good source.
 
I was involved in the testing of wandersons Entropy library ver 0.7 in June of 2013
I provided 4, 1 MB data sets from an Uno R3, an Uno clone and a Mega 2560 clone.
Wanderson emailed me and told me that my data was valid.
It was my intention, by placing the array generator in setup to force a new set every time
the device was powered up to generate a new set of PRNG based numbers in the array.
My personal intention is to use the PRNG data to insure a different offset into the array table
every time based on using the last used PRNG data to make a new randomSeed() number
available for use for choosing the next array number position in the array.
While my thoughts/methods might be incorrect..
They seem to make sense to me however not knowing how the numbers are used in the OP's code
might well invalidate my simple concept.
The Arduino thread is here:http://forum.arduino.cc/index.php/topic,108380.0.html
and the data is this:
I have generated 1,000,000 bytes of entropy on four different Arduino's so far...
Here is a summary of the initial results.

ID Device Type Sample Size Entropy Chi square P-value
1 Arduino Uno R3 DIP 1,000,000 7.999797 281.39 0.1231
3 Arduino Uno R3 DIP 1,000,000 7.999819 251.38 0.5524
3 Arduino Uno SMD 1,000,000 7.999809 265.27 0.3163
4 Arduino Mega R3 SMD 1,000,000 7.999813 258.51 0.4268

Doc
 
Last edited:
you can easy write a very fast PRNG using assembly in your code, using rotations and xor is a pretty good way to get a pretty good pseudo-randomness.
i think the noise based randomness is cool also, but for cryptography and reproductible needs, it is really not the right choice.
 
Hi all.

I've searched the forum and people have linked to Entropy for random numbers. I've tried it, and it works just fine. The main drawback is that it is insanely slow (for my needs).

Any recommends for something that works to an OK degree and can execute quickly?

thanks.
s

You could use Wanderson's entropy lib to get a good initial seed, then use your favorite PRNG/hashing function (MD5, SHA, RC4, Mersenne Twister, LFSR, LCG ...) to generate subsequent random bits. I think Wanderson had an optional interface to his library that just gets a good starting seed.
Code:
mersenne   PRNG 1000 32-bit  (microseconds)
                               TinyMT
   NXP 1170@996MHz  41 us        18  us
   T4@600MHz        67           61  
   T3.6@180MHz     462          349
   T3.5@120MHz     694          526
   T3.2@120MHz     697          527
   LC@48MHz       2341         1864
   T2++@16MHz    38680        20636
   ESP32@240MHz    349          288
   F767ZI@216MHz   210           83
   F446RE@180MHz   417          130
   32F405@168MHz   388          411
   32L476RE@80MHz  982          812    dragonfly
   pico@125MHz     797          344
   M4@120MHz       519          502    SAMD51
   artemis@96MHz   748          851
   DUE@84MHz      1519         1204    SAMD21
   maple@72MHz    1443         1114
   ZERO@48MHz     2522         2084      
   cpx@48MHz      2390         2017
mersenne.png

Mersenne Twister or TinyMT

prng_u32()

various MCU TRNGs
 
Last edited:
Yes, this is normally the way things are done. You use something slow but truly random like Entropy to seed a pseudo-random generator, and if you need crypto security, you compute 1-way hashes of small blocks of its deterministic output.

I've actually been considering replacing the Arduino core lib random() function, which is currently a copy of avr-libc's algorithm (which appears to be the Park-Miller "minimum standard") with something much better. The main one I have my eye on is Tiny Mersenne Twister.
 
Last edited:
i think the noise based randomness is cool also, but for cryptography and reproductible needs, it is really not the right choice.
I would think this would depend upon the actual application rather than the complexity of the added circuitry.

@sermad, What exactly is the "application"?

On a wild hunch I searched for reading quantum states (yes, I know reading it changes it). I found some stuff on quantum randomness being promising for ultra random generation. I just get the feeling we are like literally years, if not months, away from someone creating a micro controller with a quantum core on it. I know it is a bit off topic, but just imagine stuff like random number generating could be really fast. It also makes me wonder if the junctions on transistors are trying to tell use something. Like maybe we are getting quantum state information, but we don't know the source.
 
@edfed: Unfortunately, the cacert website doesn't provide enough information to systematically check or reproduce the results. No links to source code that I could find.
 
at least, it gives informations about the tests made on named algorithms. then, you just have to search a little to find the documentation/code. :)
for example, mine is documented on fasm board. ;)
 
My fast RNG for my Midi step sequencer. My Teensy 4.1 needs a maximum of 2 clock cycles.

Code:
static uint16_t rng_state = 1;
rng_state = (rng_state >> 1) ^ (-(rng_state & 1) & 0xb400);
 
Back
Top