Random Numbers
random is part of the Standard Library. Random will return an int. You can pass this function either 1 or 2 arguments. If you pass just one, it will return a random number between 0 (inclusive) and the number you passed in (exclusive). Passing in two numbers will return a random number between the min (inclusive) and max (exclusive).
Run this sketch a few times:
void setup(){
Serial.begin(9600);
}
void loop(){
int n=random(2,10);
Serial.println(n);
delay(500);
}
Do you notice anything funny?
Pseudo-random
The standard library random function is referred to as pseudo-random. They are not really random at all. You can seed the random number generator, which gives it a starting point. A common trick is to use an unconnected analog pin:
void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop(){
int n=random(2,10);
Serial.println(n);
delay(500);
}
This is still not really random. For really random numbers use the
TrueRandom library
#include <TrueRandom.h>
void setup() {
Serial.begin(9600);
Serial.print("I threw a random die and got ");
Serial.print(random(1,7));
Serial.print(". Then I threw a TrueRandom die and got ");
Serial.println(TrueRandom.random(1,7));
}
void loop() {
; // Do nothing
}
TrueRandom basic functions
- TrueRandom.random()
Like the Arduino library and ANSI C, this generates a random number between 0 and the highest signed long integer 2,147,483,647.
- TrueRandom.random(n)
This generates a random number between 0 and (n-1). So random(6) will generate numbers between 0 and 5.
- TrueRandom.random(a,b)
This generates a random number between a and (b-1). So random(1,7) will generate numbers between 1 and 6.
-
TrueRandom.randomBit()
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.
- TrueRandom.randomByte()
Generates a random byte between 0 and 255. Equivalent to random(256).
- TrueRandom.rand()
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.mac(address)
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.
- TrueRandom.uuid(address)
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.
Create a toy that uses TrueRandom