Close

About randomness

A project log for Dice badge

Put a bit of colorful randomness on your clothes !

pierre-loup-mPierre-Loup M. 02/22/2023 at 22:390 Comments

Here is were this project becomes a bit snob : randomness. I wanted the randomness to be as random as possible. And of course we all know that is almost impossible without a true random number generator (RNG). With the Arduino ecosystem we only have pseudo-randomness. But we can try to improve that a bit. A few years ago I read an article about achieving randomness on microcontrollers. You may have read it too, it's here. I invite you to read the article if you have not (or to read it again !), but here is the pitch : on a microcontroller, the flip-flop composing each bits of the RAM will init in a given state. On most bits it will always be the same state (i.e. 0 or 1), but on some this initial state is undefined. This uncertainty is used to seed the random number generator : on startup each and every byte of the memory is read, shifted as needed and xor'ed with the previous read. Once the memory has been read, this number is used as the seed.

The other part of randomness is the algorithm used. After reading a few articles on wikipedia (and understanding very few of them) I implemented an XORshift algorithm for generating random numbers. Arduino's random was probably good enough, but that's a bit of accomplishment to add to the project. :)

Xorshift is quite good and only needs four lines of code. You can read about it on wikipedia.

Discussions