Statistical Tests for My Audio Based Random Number Generator

In May I’ve written about a way to generate random number from audio noise. Basically it went like this:

  1. Get audio sample from the microphone.
  2. Push the least significant bit to a buffer.
  3. Repeat steps 1-2 until the buffer is full (buffer size == block size for the hash function).
  4. Apply the hash function on the buffer.
  5. 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
 * 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
 * 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 <>.

#include <cstdio>
#include "md5.h"

class Grandom {
		virtual ~Grandom();
		 * Generate a random dword
		uint32_t operator()();
		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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.