Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Generating random numbers using NAudio
- ======================================
- I have written a C# class, SoundRandom, which may be used to generate small quantities of random numbers. It is simple to use...
- 1) Create a new instance of the class:
- SoundRandom sr = new SoundRandom();
- 2) An unlimited sequence of random uints may then be collected by repeatedly
- getting the RandomUint property:
- sr.RandomUint
- Such random uints, which are truly random, will be generated at the rate of about
- one thousand per second. These values may then be used, for example, to create
- seeds for some deterministic random number generator.
- The program extracts the least significant bits from successive samples from
- a microphone socket. No microphone needs to be plugged into the socket.
- By default, the program sets the audio sample rate to 100,000 Hz, in stereo.
- The program uses a traditional Von Neumann randomness extractor on the
- bit stream to create the random uints.
- So, a simple demo program to create ten random uints and display them in
- hexadecimal might be as follows:
- SoundRandom sr = new SoundRandom();
- for (int i = 0; i < 10; i++)
- {
- Tell(sr.RandomUint.ToString("x8"));
- }
- Producing the following output, perhaps:
- 5b6525e3
- 083d5a7a
- dddf47f2
- 0fee1071
- 1d8b4e65
- 83027594
- 58f18820
- c0afaac9
- 92e61b46
- 0fb6b399
- */
- public class SoundRandom
- {
- private static readonly int bitsPerUint = 32;
- private EventHandler<WaveInEventArgs> bufferHandler = null;
- private int channels = 0;
- private ManualResetEvent bufferReady = new ManualResetEvent(false);
- private int nextI = 0;
- private int randomBufferSize = 0;
- private int randomCount = 0;
- private uint[] randoms = null;
- private uint randomUint = 0;
- private int randomUintBitCount = 0;
- private int sampleRate = 0;
- private WaveInEvent waveInStream = null;
- public SoundRandom(
- int randomBufferSize_param,
- int sampleRate_param,
- int channels_param
- )
- {
- randomBufferSize = randomBufferSize_param;
- sampleRate = sampleRate_param;
- channels = channels_param;
- nextI = randomBufferSize;
- randoms = new uint[randomBufferSize];
- } // ctor int int int
- public SoundRandom() : this(1000, 100000, 2)
- {
- //
- } // ctor
- public uint RandomUint
- {
- get
- {
- if (nextI >= randomBufferSize)
- {
- StartCapture();
- bufferReady.WaitOne();
- bufferReady.Reset();
- }
- return randoms[nextI++];
- } // get
- } // RandomUint
- private void HandleNextBuffer(object sender, WaveInEventArgs e)
- {
- byte[] buffer = e.Buffer;
- int bytes = e.BytesRecorded;
- int count = bytes/4;
- for (int i = 0; i < count; i++)
- {
- int j = i*4;
- uint b1 = (uint)(buffer[j] & 1);
- uint b2 = (uint)(buffer[j+2] & 1);
- if (b1 != b2)
- {
- randomUint = (randomUint << 1) | b2;
- ++randomUintBitCount;
- if (randomUintBitCount == bitsPerUint)
- {
- randomUintBitCount = 0;
- if (randomCount >= 0)
- {
- randoms[randomCount] = randomUint;
- }
- randomUint = 0;
- ++randomCount;
- if (randomCount == randomBufferSize)
- {
- waveInStream.DataAvailable -= bufferHandler;
- waveInStream.StopRecording();
- waveInStream.Dispose();
- waveInStream = null;
- bufferReady.Set();
- return;
- }
- }
- }
- } // for i
- } // HandleNextBuffer
- public void StartCapture()
- {
- bufferHandler = new EventHandler<WaveInEventArgs>(HandleNextBuffer);
- nextI = 0;
- randomCount = -1;
- randomUint = 0;
- randomUintBitCount = 0;
- waveInStream = new WaveInEvent();
- waveInStream.WaveFormat = new WaveFormat(sampleRate, channels);
- waveInStream.DataAvailable += bufferHandler;
- waveInStream.StartRecording();
- } // StartCapture
- } // SoundRandom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement