- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/GeigerRandom/GeigerRandom.cs

    00001 ﻿//-----------------------------------------------------------------------------
    00002 // GeigerRandom.cs
    00003 // (A. Schiffler, 2009-2011)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace GeigerRandom
    00007 {
    00008     using System;
    00009     using System.IO;
    00010     using NewGamePhysics.Devices;
    00011     using NewGamePhysics.Mathematics;
    00012     using NewGamePhysics.Utilities;
    00013
    00020     class GeigerRandom
    00021     {
    00022         private const int arraySize = 1024;
    00023         private static int arrayPos = 0;
    00024
    00025         private static double lastTime;
    00026         private static double[] data = new double[arraySize];
    00027         private static ValueUnbiasAlgorithm unbiasAlgorithm = ValueUnbiasAlgorithm.Partition;
    00028         private static ValueUnbiaser unbiaser = new ValueUnbiaser(unbiasAlgorithm);
    00029         private static int totalBits = 0;
    00030
    00031         private static string dataAscii = "data.txt";
    00032         private static string dataBytes = "data.dat";
    00033
    00034         static void Main(string[] args)
    00035         {
    00036             System.Console.Error.WriteLine("=== Aware Geiger Random Generator ===");
    00037             System.Console.Error.WriteLine(" Unbias algorithm: " + unbiasAlgorithm);
    00038             System.Console.Error.WriteLine(" Block size: " + arraySize);
    00039
    00040             if (File.Exists(dataAscii))
    00041             {
    00042                 File.Delete(dataAscii);
    00043             }
    00044
    00045             if (File.Exists(dataBytes))
    00046             {
    00047                 File.Delete(dataBytes);
    00048             }
    00049
    00050             try
    00051             {
    00052                 AwareGeigerCounter awareCounter = new AwareGeigerCounter(args[0]);
    00053                 lastTime = HighResolutionTimer.Seconds();
    00054                 awareCounter.EventHandler = CounterEventHandler;
    00055             }
    00056             catch (Exception e)
    00057             {
    00058                 System.Console.Error.WriteLine("Error: " + e.Message);
    00059                 System.Console.Error.WriteLine(e);
    00060                 return;
    00061             }
    00062
    00063             System.Console.Error.WriteLine("Press key to stop ...");
    00064             System.Console.ReadKey(false);
    00065         }
    00066
    00071         private static void CounterEventHandler(double relativeTime)
    00072         {
    00073             // Capture relative time
    00074             data[arrayPos] = relativeTime;
    00075             arrayPos++;
    00076             if (arrayPos == arraySize)
    00077             {
    00078                 string bits = unbiaser.Extract(data);
    00079
    00080                 // Write bytes
    00081                 byte accumulator = 0;
    00082                 int bitcount = 0;
    00083                 FileStream fs = File.Open(dataBytes, FileMode.OpenOrCreate | FileMode.Append);
    00084                 BinaryWriter br = new BinaryWriter(fs);
    00085                 foreach (char bit in bits)
    00086                 {
    00087                     accumulator *= 2;
    00088                     accumulator |= (bit == '0') ? (byte)0 : (byte)1;
    00089                     bitcount++;
    00090                     if (bitcount == 8)
    00091                     {
    00092                         br.Write(accumulator);
    00093                         bitcount = 0;
    00094                         accumulator = 0;
    00095                     }
    00096                 }
    00097                 br.Close();
    00098                 fs.Close();
    00099
    00100                 // Write ASCII
    00101                 fs = File.Open(dataAscii, FileMode.OpenOrCreate | FileMode.Append);
    00102                 TextWriter sr = new System.IO.StreamWriter(fs);
    00103                 sr.Write(bits);
    00104                 sr.Close();
    00105                 fs.Close();
    00106
    00107                 totalBits += bits.Length;
    00108                 System.Console.Error.WriteLine("Total Bits = {0}   Current Bits = {1} from {2} samples    Yield = {3}%", totalBits, bits.Length, arraySize, 100.0 * (double)bits.Length / (double)arraySize);
    00109                 arrayPos = 0;
    00110             }
    00111         }
    00112     }
    00113 }