This project has moved. For the latest updates, please go here.

Generating random numbers using NAudio

Mar 13, 2013 at 9:47 AM
Here is some C# source code that can be used to generate random 32-bit unsigned integers.

http://pastebin.com/vmhGzKP7

It could form the basis for an official NAudio method - assuming that such a method does not already exist.

~~
Clive Tooth
Coordinator
Mar 13, 2013 at 3:37 PM
hi, that's certainly an interesting approach to creating random numbers. I would be worried that a soundcard with a noise gate would end up creating a sequence of all 0 values. It's not really the type of feature that I would include in NAudio, as NAudio is focused on audio tasks rather than general programming utilities, but thanks for sharing your code.

best regards

Mark
Mar 13, 2013 at 5:20 PM
Hi Mark,

I take your point about NAudio not being focused on general programming utilities.

As regards creating a sequence of all zeros, the program guards against this as follows:

The program considers successive pairs of 16-bit samples from the audio buffer. It looks at the least significant bit from each sample. If the bits are the same the program discards this data and moves on to the next pair of samples. However, if the two bits are different, the program appends the second bit to the growing stream of random bits - and then moves on to the next pair of samples. [This is the line "if (b1 != b2)" in HandleNextBuffer.] This process is called a John von Neumann randomness extractor. Here is an example from running the code on my computer. The first two columns are the pairs of 16-bit samples and the sequence of 0's and 1's on the right hand side is the sequence of random bits. If every sample was 0000 the program would get stuck and never produce a single random bit.

0002 0002: least significant bits: 00 discard
fffe 0001: least significant bits: 01 generate 1
fffd fffe: least significant bits: 10 generate 0
ffff fffd: least significant bits: 11 discard
0001 fffe: least significant bits: 10 generate 0
0002 0001: least significant bits: 01 generate 1
0001 0000: least significant bits: 10 generate 0
fffe fffd: least significant bits: 01 generate 1
fffc fff9: least significant bits: 01 generate 1
fffc fff8: least significant bits: 00 discard
0001 fffa: least significant bits: 10 generate 0
0002 fffd: least significant bits: 01 generate 1
0002 fffe: least significant bits: 00 discard
0000 fffd: least significant bits: 01 generate 1
ffff fffc: least significant bits: 10 generate 0
0001 fffc: least significant bits: 10 generate 0
0001 fffd: least significant bits: 11 discard
0000 fffe: least significant bits: 00 discard
fffe fffe: least significant bits: 00 discard
fffd fffe: least significant bits: 10 generate 0
ffff fffe: least significant bits: 10 generate 0
0004 0000: least significant bits: 00 discard
0006 fffe: least significant bits: 00 discard
0005 fffd: least significant bits: 11 discard
0002 fffe: least significant bits: 00 discard
0003 0001: least significant bits: 11 discard
0005 0003: least significant bits: 11 discard
0005 0001: least significant bits: 11 discard
0001 ffff: least significant bits: 11 discard
fffd fffc: least significant bits: 10 generate 0
fffd fffd: least significant bits: 11 discard
0001 fffe: least significant bits: 10 generate 0

... and so on.

Clive