2using System.Collections.Generic;
5using System.Security.Cryptography;
15 private static readonly RNGCryptoServiceProvider m_rand =
new RNGCryptoServiceProvider();
16 private static Random m_rand1 =
new Random();
17 private IndexCollection m_rgIdx =
null;
58 if (m_method ==
METHOD.CRYPTO)
61 m_rand1 =
new Random(nSeed);
65#pragma warning disable 1591
67 public override void GetBytes(
byte[] data)
69 m_rand.GetBytes(data);
72 public override void GetNonZeroBytes(
byte[] data)
74 m_rand.GetNonZeroBytes(data);
77#pragma warning restore 1591
85 if (m_method ==
METHOD.CRYPTO)
87 byte[] rgb =
new byte[
sizeof(UInt64)];
89 return (
double)BitConverter.ToUInt64(rgb, 0) / (double)UInt64.MaxValue;
92 return m_rand1.NextDouble();
103 return (
NextDouble() * (dfMax - dfMin)) + dfMin;
113 public int Next(
int nMinVal,
int nMaxVal,
bool bMaxInclusive =
true)
115 int nVal = (int)Math.Round((
NextDouble() * ((
double)nMaxVal - nMinVal)) + nMinVal);
132 return Next(
int.MaxValue);
142 if (m_method ==
METHOD.UNIFORM_EXACT)
145 m_rgIdx =
new IndexCollection(nMaxVal);
146 else if (nMaxVal != m_rgIdx.Count)
147 throw new Exception(
"CryptoRandom: The maximum count has changed!");
149 IndexCollection rgMin = m_rgIdx.GetMinumums();
150 int nIdx =
Next(0, rgMin.Count-1);
152 nIdx = rgMin.Index(nIdx);
156 return Next(0, nMaxVal - 1);
160 class IndexCollection
164 public IndexCollection(
int nCount)
166 m_rgItems =
new Item[nCount];
168 for (
int i = 0; i < nCount; i++)
170 m_rgItems[i] =
new Item(i);
174 public IndexCollection(Item[] rg)
181 get {
return m_rgItems.Length; }
184 public int Add(
int nIdx)
186 m_rgItems[nIdx].Count++;
187 return m_rgItems[nIdx].Count;
190 public int Index(
int nIdx)
192 m_rgItems[nIdx].Count++;
193 return m_rgItems[nIdx].Index;
196 public IndexCollection GetMinumums()
198 int nMinCount =
int.MaxValue;
200 for (
int i = 0; i < m_rgItems.Length; i++)
202 if (m_rgItems[i].Count < nMinCount)
203 nMinCount = m_rgItems[i].Count;
206 Item[] rgMin = m_rgItems.Where(p => p.Count == nMinCount).ToArray();
207 return new IndexCollection(rgMin);
216 public Item(
int nIdx)
224 get {
return m_nIdx; }
229 get {
return m_nCount; }
230 set { m_nCount = value; }
233 public override string ToString()
235 return m_nIdx.ToString() +
" -> " + m_nCount.ToString();
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
METHOD
Defines the random number generation method to use.
int Next(int nMaxVal)
Returns a random int within the range , where the random number is less than nMaxVal.
int Next(int nMinVal, int nMaxVal, bool bMaxInclusive=true)
Returns a random int within the range
double NextDouble()
Returns a random double within the range .
int Next()
Returns a random int within the range
CryptoRandom(METHOD method=METHOD.DEFAULT, int nSeed=0)
The CryptoRandom constructor.
double NextDouble(double dfMin, double dfMax)
Returns a random double within the range
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
@ DEFAULT
Specifies to use the default data type of the gym used.