The Raspberry Pi (A/A+/B/B+/and 2) includes a hardware based random number generator that promises to provide the fastest, best source for the typical users to obtain cryptographic quality random numbers in an extremely small portable package.
The following steps should be performed to get the hardware random number generator up and running:
Install the random number generator tools (sudo apt-get install rng-tools)
Add the hwrng module to the boot process (add line ‘bcm2708-rng’ to file /etc/modules)
Reboot the machine
/dev/hwrng is now available for reading (root access only)
sudo chmod a+r /dev/hwrng (gives user lever read access). This only stays until machine rebooted!
To make the change permanent, make the following change in the /etc/rc.local file:
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.
# Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi
# ADD this line just above the exit 0 command chmod a+r /dev/hwrng exit 0
One way to generate a 1 megabyte file with random numbers (bytes) is to use the following command:
As you can see from the above detailed analysis of the samples, the data behaves exactly as we would expect a true random number generator to perform, it occasionally fails some tests, but those failures are not duplicated on subsequent samples indicating the failuresare just examples of random chance as one would expect. Overall the built in hardware random generator on the Raspberry Pi appears to perform quite well, and at 1 million bits per second is likely the most affordable true random number generator available to the general public.
Using the generator
The dd command works well for creating a binary file of random numbers; however, that is less than useful for most applications. Since I am in the process of setting up a Pi Cluster, I thought it would be nice to create a class to encapsulate obtaining random numbers in a variety of formats. Ultimately I am planning on producing software that will make use of the Raspberry Pi cluster to serve up random numbers to other machines that will make use of them for things like simulations. Until then here is the class that encapsulates the random number generator.
The class library to use the Raspberry Pi hardware random number generator is trng.h This class contains only four methods.
trng.random()
This method will return a full range (32 or 64 bit) random number depending upon the data type used. For integers, the number is self-explanatory. For real numbers (float and double) the value will be between 0 and 1.
trng.random(max)
This method will return a random number between 0 and the specified max.
trng.random(min,max)
This method will return a random number between min and max
trng.rnorm(mean, stdDev)
This method will return a random number with a gaussian (normal, bell-curve) distribution that has the specified mean and standard deviation, if and only if the data type is a float or a double. For all other types this method will always return a 0.
Example of how to use
// test-suite.cpp
//
// Copyright 2014 by Walter Anderson
//
// This program is a basic example and test-suite of how to use the
// trng class on a Raspberry Pi to obtain true random numbers in a
// variety of forms.
//
// This is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Entropy. If not, see <http://www.gnu.org/licenses/>.
#include <iostream>
#include <iomanip>
#include "trng.h"