- Main Page
- Classes
- Files

- File List

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

    00001 ﻿using System;
    00002 using System.Collections.Generic;
    00003 using System.Linq;
    00004 using System.Text;
    00005
    00006 using NewGamePhysics.Networking;
    00007
    00008 namespace NewGamePhysics.PhysicalElements
    00009 {
    00013     public enum EntropySourceType
    00014     {
    00019         Pseudo,
    00020
    00025         PlayTrulyRandom
    00026     }
    00027
    00032     public class PhysicalRandomNumberGenerator
    00033     {
    00037         private EntropySourceType entropySource =
    00038             EntropySourceType.Pseudo;
    00039
    00043         private string agentName =
    00044             string.Empty;
    00045
    00049         private int retrievedEntropyBits;
    00050
    00054         private Random prng =
    00055             new Random();
    00056
    00060         private PlayTrulyRandom playTrulyRandom;
    00061
    00065         public PlayTrulyRandom PlayTrulyRandom
    00066         {
    00067             get { return this.playTrulyRandom; }
    00068         }
    00069
    00073         public EntropySourceType EntropySource
    00074         {
    00075             get { return this.entropySource; }
    00076             set {
    00077                 this.entropySource = value;
    00078
    00079                 // Re-initialize
    00080                 this.Initialize();
    00081             }
    00082         }
    00083
    00087         public string AgentName
    00088         {
    00089             get { return this.agentName; }
    00090         }
    00091
    00095         public int RetrievedEntropyBits
    00096         {
    00097             get { return this.retrievedEntropyBits; }
    00098             set { this.retrievedEntropyBits = value; }
    00099         }
    00100
    00107         public PhysicalRandomNumberGenerator(
    00108             EntropySourceType source,
    00109             string agentName)
    00110         {
    00111             // Keep state
    00112             this.entropySource = source;
    00113             this.agentName = agentName;
    00114
    00115             // Initialization
    00116             this.Initialize();
    00117         }
    00118
    00131         public int Next(int minValue, int maxValue)
    00132         {
    00133             if (maxValue < minValue)
    00134             {
    00135                 throw new ArgumentException(
    00136                     "maxValue must be greater or equals than minValue",
    00137                     "maxValue");
    00138             }
    00139
    00140             int result = 0;
    00141
    00142             // Generate based on entropy source type
    00143             switch (this.entropySource)
    00144             {
    00145                 // OS based pseudo generator
    00146                 case EntropySourceType.Pseudo:
    00147                     // Adjust for exclusive range of .Next
    00148                     if (maxValue < int.MaxValue)
    00149                     {
    00150                         maxValue++;
    00151                     }
    00152
    00153                     result = this.prng.Next(minValue, maxValue);
    00154                     this.retrievedEntropyBits += 32;
    00155                     break;
    00156
    00157                 // PTR webservice
    00158                 case EntropySourceType.PlayTrulyRandom:
    00159                     try
    00160                     {
    00161                         int retrievedBits;
    00162                         result = playTrulyRandom.Next(minValue, maxValue, out retrievedBits);
    00163                         this.retrievedEntropyBits += retrievedBits;
    00164                     }
    00165                     catch (Exception)
    00166                     {
    00167                         // On error, fall back on pseudo random number generator
    00168                         this.entropySource = EntropySourceType.Pseudo;
    00169                         result = this.Next(minValue, maxValue);
    00170                     }
    00171
    00172                     break;
    00173             }
    00174
    00175             return result;
    00176         }
    00177
    00181         private void Initialize()
    00182         {
    00183             // Always initialize pseudo random number generator
    00184             this.prng = new Random();
    00185
    00186             // Initialize based on type
    00187             switch (this.entropySource)
    00188             {
    00189                 case EntropySourceType.PlayTrulyRandom:
    00190                     this.playTrulyRandom = new PlayTrulyRandom(agentName);
    00191                     try
    00192                     {
    00193                         // Register with webservice
    00194                         this.playTrulyRandom.RegisterAgent();
    00195                     }
    00196                     catch
    00197                     {
    00198                         // On error, fall back on pseudo random number generator
    00199                         this.entropySource = EntropySourceType.Pseudo;
    00200                     }
    00201
    00202                     break;
    00203             }
    00204         }
    00205     }
    00206 }