My favorites | Sign in
Project Home Downloads Wiki Issues Source
TrueRandom library for Arduino
Updated Mar 31, 2010 by


TrueRandom generates true random numbers on Arduino. They are different every time you start your program, and are truly unpredictable unlike the default Arduino random() function.


TrueRandom currently functions on the Arduino Diecimila, Duemilanove, 168 and 328 based Arduinos. It does not yet function on the Arduino Mega. TrueRandom uses Analog 0. Do not connect anything to this pin. These restrictions may be removed in future versions of this library.


Download TrueRandom library. Extract the zip file, and copy the directory to your Arduino libraries folder.

What happens when you use the Arduino random() function?

The Arduino default random() function generates what appear to be random numbers. They are actually calculated from a formula. On reset, the formula is reset at a start point, then progresses through a long sequence of random looking numbers. However, Arduino starts at the same point in the sequence every reset. You can move to a different part of the sequence using srandom(), but how do you get a random start point from in the first place?

What happens when you use TrueRandom.random() function?

You get a random number. Really random. Different every time you restart.

Example time

#include <TrueRandom.h>

void setup() {

  Serial.print("I threw a random die and got ");

  Serial.print(". Then I threw a TrueRandom die and got ");


void loop() {
  ; // Do nothing

Upload that code to an Arduino Duemilanove and watch it on the Serial Monitor at 9600 baud. Hit the reset button, and see what it does. The random() function returns the same value every time, but the TrueRandom version is always different.

TrueRandom basic functions

The existing random functions of Arduino are replicated in TrueRandom.


Like the Arduino library and ANSI C, this generates a random number between 0 and the highest signed long integer 2,147,483,647.


This generates a random number between 0 and (n-1). So random(6) will generate numbers between 0 and 5.


This generates a random number between a and (b-1). So random(1,7) will generate numbers between 1 and 6.

TrueRandom advanced functions


Generating true random numbers takes time, so it can be useful to only generate as many random bits as you need. randomBit() generates a 0 or a 1 with 50% probability. This is the core function from which the other TrueRandom libraries are built.


Generates a random byte between 0 and 255. Equivalent to random(256).


Like the ANSI C rand() command, this generates a random number between 0 and the highest signed integer 32767.

TrueRandom.memfill(address, length)

Fills a block of bytes with random numbers. (length) bytes are filled in total, starting at the given (address).

TrueRandom specialist functions


When operating devices on an Ethernet network, each device must have a unique MAC address. Officially, MAC addresses should be assigned formally via the IEEE Registration Authority. However, for practical purposes, MAC addresses can be randomly assigned without problems. This function writes a 6 byte MAC address to a given address. Randomly generated MAC addresses are great for projects or workshops involving large numbers of Arduino Ethernet shields, as each shield has a different MAC address, even though they are running identical code. See the MacAddress example which shows this in use.


UUIDs are unique identifiers. They are 16 bytes (128 bits) long, which means that generating them randomly This generates a random UUID, and writes it to an array. UUIDs are globally unique numbers that are often used in web services and production electronics. TrueRandom can produce any one of 5,316,911,983,139,663,491,615,228,241,121,378,304 different numbers. You're more likely to win top prize in the national lottery 3 times in a row than get two matching UUIDs.

How TrueRandom works

It is hard to get a truly random number from Arduino. TrueRandom does it by setting up a noisy voltage on Analog pin 0, measuring it, and then discarding all but the least significant bit of the measured value. However, that isn't noisy enough, so a von Neumann whitening algorithm gathers enough entropy from multiple readings to ensure a fair distribution of 1s and 0s.

The other functions within TrueRandom construct the requested values by gathering just enough random bits to produce the required numbers. Generating a random bit takes time, so a significant part of the code works to ensure the random bits are used as efficiently as possible.

Projects using TrueRandom

Generative Music from Gijs

Comment by, Aug 2, 2011

Amazing! Thank you so much! I needed a random number on every reboot of my arduino, but not during the program, and it was the same everytime. This worked perfectly!

Comment by, Nov 5, 2011

I've just done some randomness test using NIST's (National Institute of Standards and Technology) statistical test suite (available at against "random" bit streams generated by this library and the result is terrible. It does not pass through any of the 15 statistical tests performed by this tool. A trully random bit stream is suppoded to pass through all of them.

Comment by, Apr 4, 2012

It may have failed NIST's test, but this library is way better than the Arduino's random generator.

Comment by, Apr 15, 2012

"TrueRandom does it by setting up a noisy voltage on Analog pin 0, measuring it, and then discarding all but the least significant bit of the measured value."

Why are you throwing away entropy?? o_O

Comment by, Jul 14, 2012

Hi, I'm trying to modify the library to use an analog pin other than 0, as I'm also using an LCD shield which uses Analog 0 and can't be changed. Can anyone go through the library and suggest what to change? (I've tried but with errors). I also want to use an external white noise generator to this other pin - anything I need to change for that? Thank you.

Comment by, Feb 4, 2013

I made a (probably not super-scientific) test and it looks to me that there is a slight bias for the 0:

#include <TrueRandom.h>

long count=0; long counts24?;

void setup() {



void loop() {

int v = TrueRandom.random(24);
countsv?++; count++;

if ( (count % 10000) == 0 ) {

for ( int i = 0; i < 24; i++ ) {
Serial.print(i,DEC); Serial.print(": "); Serial.print(countsi?,DEC);
Serial.print(" ");
Serial.print(countsi? 100 / count,DEC);
Serial. println("%"); countsi? = 0;
} count = 0;
Serial.println(" ");
} }

This creates 10000 random numbers between 0 and 24 and counts their occurrence. When you look at the results you can see that in about 95% of all results the 0 appears most often. I would expect that there is more of a variety in which number is the "winner".

Comment by, Feb 4, 2013

Yeah, don't use "TrueRandom" if you want a truly random number. It's a poor random number generator, producing more 0s than 1s:

Here's a better one:

Comment by, Apr 30, 2014

For an Arduino library that provide truly random numbers on all platforms that has been tested on over a hundred different devices, including the new Arduino Due, you can use; and if you just want to create a random seed for the built-in random function you can find a sketch to show how here;

Sign in to add a comment
Powered by Google Project Hosting