MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
CryptoRandom.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Security.Cryptography;
6
7namespace MyCaffe.basecode
8{
13 public class CryptoRandom : RandomNumberGenerator
14 {
15 private static readonly RNGCryptoServiceProvider m_rand = new RNGCryptoServiceProvider();
16 private static Random m_rand1 = new Random();
17 private IndexCollection m_rgIdx = null;
18
19 METHOD m_method = METHOD.DEFAULT;
20
24 public enum METHOD
25 {
29 SYSTEM,
36 CRYPTO,
40 UNIFORM_EXACT,
44 DEFAULT = CRYPTO
45 }
46
52 public CryptoRandom(METHOD method = METHOD.DEFAULT, int nSeed = 0)
53 {
54 m_method = method;
55
56 if (nSeed != 0)
57 {
58 if (m_method == METHOD.CRYPTO)
59 m_method = METHOD.SYSTEM;
60
61 m_rand1 = new Random(nSeed);
62 }
63 }
64
65#pragma warning disable 1591
66
67 public override void GetBytes(byte[] data)
68 {
69 m_rand.GetBytes(data);
70 }
71
72 public override void GetNonZeroBytes(byte[] data)
73 {
74 m_rand.GetNonZeroBytes(data);
75 }
76
77#pragma warning restore 1591
78
83 public double NextDouble()
84 {
85 if (m_method == METHOD.CRYPTO)
86 {
87 byte[] rgb = new byte[sizeof(UInt64)];
88 m_rand.GetBytes(rgb);
89 return (double)BitConverter.ToUInt64(rgb, 0) / (double)UInt64.MaxValue;
90 }
91
92 return m_rand1.NextDouble();
93 }
94
101 public double NextDouble(double dfMin, double dfMax)
102 {
103 return (NextDouble() * (dfMax - dfMin)) + dfMin;
104 }
105
113 public int Next(int nMinVal, int nMaxVal, bool bMaxInclusive = true)
114 {
115 int nVal = (int)Math.Round((NextDouble() * ((double)nMaxVal - nMinVal)) + nMinVal);
116
117 if (!bMaxInclusive)
118 {
119 if (nVal == nMaxVal)
120 nVal--;
121 }
122
123 return nVal;
124 }
125
130 public int Next()
131 {
132 return Next(int.MaxValue);
133 }
134
140 public int Next(int nMaxVal)
141 {
142 if (m_method == METHOD.UNIFORM_EXACT)
143 {
144 if (m_rgIdx == null)
145 m_rgIdx = new IndexCollection(nMaxVal);
146 else if (nMaxVal != m_rgIdx.Count)
147 throw new Exception("CryptoRandom: The maximum count has changed!");
148
149 IndexCollection rgMin = m_rgIdx.GetMinumums();
150 int nIdx = Next(0, rgMin.Count-1);
151
152 nIdx = rgMin.Index(nIdx);
153 return nIdx;
154 }
155
156 return Next(0, nMaxVal - 1);
157 }
158 }
159
160 class IndexCollection
161 {
162 Item[] m_rgItems;
163
164 public IndexCollection(int nCount)
165 {
166 m_rgItems = new Item[nCount];
167
168 for (int i = 0; i < nCount; i++)
169 {
170 m_rgItems[i] = new Item(i);
171 }
172 }
173
174 public IndexCollection(Item[] rg)
175 {
176 m_rgItems = rg;
177 }
178
179 public int Count
180 {
181 get { return m_rgItems.Length; }
182 }
183
184 public int Add(int nIdx)
185 {
186 m_rgItems[nIdx].Count++;
187 return m_rgItems[nIdx].Count;
188 }
189
190 public int Index(int nIdx)
191 {
192 m_rgItems[nIdx].Count++;
193 return m_rgItems[nIdx].Index;
194 }
195
196 public IndexCollection GetMinumums()
197 {
198 int nMinCount = int.MaxValue;
199
200 for (int i = 0; i < m_rgItems.Length; i++)
201 {
202 if (m_rgItems[i].Count < nMinCount)
203 nMinCount = m_rgItems[i].Count;
204 }
205
206 Item[] rgMin = m_rgItems.Where(p => p.Count == nMinCount).ToArray();
207 return new IndexCollection(rgMin);
208 }
209 }
210
211 class Item
212 {
213 int m_nIdx;
214 int m_nCount;
215
216 public Item(int nIdx)
217 {
218 m_nIdx = nIdx;
219 m_nCount = 0;
220 }
221
222 public int Index
223 {
224 get { return m_nIdx; }
225 }
226
227 public int Count
228 {
229 get { return m_nCount; }
230 set { m_nCount = value; }
231 }
232
233 public override string ToString()
234 {
235 return m_nIdx.ToString() + " -> " + m_nCount.ToString();
236 }
237 }
238}
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
Definition: CryptoRandom.cs:14
METHOD
Defines the random number generation method to use.
Definition: CryptoRandom.cs:25
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 .
Definition: CryptoRandom.cs:83
int Next()
Returns a random int within the range
CryptoRandom(METHOD method=METHOD.DEFAULT, int nSeed=0)
The CryptoRandom constructor.
Definition: CryptoRandom.cs:52
double NextDouble(double dfMin, double dfMax)
Returns a random double within the range
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ DEFAULT
Specifies to use the default data type of the gym used.