In May I’ve written about a way to generate random number from audio noise. Basically it went like this:
- Get audio sample from the microphone.
- Push the least significant bit to a buffer.
- Repeat steps 1-2 until the buffer is full (buffer size == block size for the hash function).
- Apply the hash function on the buffer.
- Get random bits from the digest.
In order to continue developing this random number generator (RNG), I’ve written a C++ class that simplifies working with it.
/*
* Copyright (C) 2010 Guy Rutenberg
* http://www.guyrutenberg.com
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include "md5.h"
class Grandom {
public:
Grandom();
virtual ~Grandom();
/**
* Generate a random dword
*/
uint32_t operator()();
private:
void gather_entropy();
void get_block();
FILE* m_dsp_fd;
uint32_t m_index;
union {
char digest[16];
uint32_t v[4];
} m_buffer;
md5_ctx m_md5_ctx;
uint32_t m_block[512/32];
};
The Grandom
class uses MD5 as the hash function, 16-bit samples and 44.1KHz sampling rate. This configuration should work well on any modern sound card. The full implementation is available in grandom-1.0.tar.bz2
, which is a small test app.
To have some evidence for the quality of the random number generation I’ve tested it against NIST’s statistical test suite. I’ve tested 320 streams of 1M bits each. The results showed that the random number generator passed all of the tests but one (one of the non-overlapping pattern test, although it was very close to pass). If you wan to see the results, take a look at the final analysis report.
Overall, Grandom
seems to be a good way to generate small amounts of high quality random numbers.