Skip to content

Instantly share code, notes, and snippets.

@HurricanKai
Created September 12, 2018 14:41
Show Gist options
  • Save HurricanKai/95ff401ea6b22591e581cce4415d7279 to your computer and use it in GitHub Desktop.
Save HurricanKai/95ff401ea6b22591e581cce4415d7279 to your computer and use it in GitHub Desktop.
Testing RNGCryptoServiceProvider
using CsvHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace SecurityGame
{
class Program
{
public static Level[] Levels = new Level[]
{
new Level0(),
new Level1(),
};
static void Main(string[] args)
{
using (var t = new StreamWriter(File.Create("./table.csv")))
{
using (var csv = new CsvWriter(t))
{
const int c = 2000000000;
var stats = new int[Byte.MaxValue + 1];
for (int i = 0; i < stats.Length; i++)
stats[i] = 0;
RNGCryptoServiceProvider p = new RNGCryptoServiceProvider();
Parallel.For(0, c, x =>
{
var buff = new Byte[1];
p.GetBytes(buff);
stats[buff[0]]++;
});
var expected = (float)(c / Byte.MaxValue);
/*Console.WriteLine("Expected Value " + expected);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("absolute Value");
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < stats.Length; i++)
Console.WriteLine($"{i.ToString("000")} | {stats[i].ToString("00000000")}");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("absolute Error");
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < stats.Length; i++)
Console.WriteLine($"{i.ToString("000")} | {(stats[i] - expected).ToString("R")}");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("% Error");
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < stats.Length; i++)
Console.WriteLine($"{i.ToString("000")} | {((stats[i] - expected) / expected).ToString("P")}");
*/
var table = new List<object>();
for (int i = 0; i < stats.Length; i++)
{
table.Add(new { Value = i,
absolute_Value = stats[i],
absolute_Error = (stats[i] - expected),
percentual_Error = ((stats[i] - expected) / expected)
});
}
csv.WriteRecords(table);
csv.Flush();
Console.WriteLine("ok.");
}
}
Console.ReadLine();
}
}
}
Value absolute_Value absolute_Error percentual_Error
000 07812782 -30355 -0,39
001 07808698 -34439 -0,44
002 07814114 -29023 -0,37
003 07813364 -29773 -0,38
004 07813385 -29752 -0,38
005 07814613 -28524 -0,36
006 07808894 -34243 -0,44
007 07809286 -33851 -0,43
008 07814553 -28584 -0,36
009 07811441 -31696 -0,40
010 07812512 -30625 -0,39
011 07808047 -35090 -0,45
012 07812852 -30285 -0,39
013 07816168 -26969 -0,34
014 07809848 -33289 -0,42
015 07813432 -29705 -0,38
016 07813663 -29474 -0,38
017 07808839 -34298 -0,44
018 07814538 -28599 -0,36
019 07816967 -26170 -0,33
020 07809129 -34008 -0,43
021 07813191 -29946 -0,38
022 07812083 -31054 -0,40
023 07813044 -30093 -0,38
024 07812576 -30561 -0,39
025 07810256 -32881 -0,42
026 07811354 -31783 -0,41
027 07814716 -28421 -0,36
028 07818263 -24874 -0,32
029 07807403 -35734 -0,46
030 07809351 -33786 -0,43
031 07807899 -35238 -0,45
032 07811900 -31237 -0,40
033 07815346 -27791 -0,35
034 07815943 -27194 -0,35
035 07813196 -29941 -0,38
036 07809047 -34090 -0,43
037 07811917 -31220 -0,40
038 07811501 -31636 -0,40
039 07810972 -32165 -0,41
040 07818495 -24642 -0,31
041 07811641 -31496 -0,40
042 07810090 -33047 -0,42
043 07807669 -35468 -0,45
044 07808184 -34953 -0,45
045 07814431 -28706 -0,37
046 07816835 -26302 -0,34
047 07812920 -30217 -0,39
048 07813161 -29976 -0,38
049 07813177 -29960 -0,38
050 07813037 -30100 -0,38
051 07813316 -29821 -0,38
052 07814604 -28533 -0,36
053 07810179 -32958 -0,42
054 07809552 -33585 -0,43
055 07808006 -35131 -0,45
056 07813772 -29365 -0,37
057 07809500 -33637 -0,43
058 07810870 -32267 -0,41
059 07819180 -23957 -0,31
060 07812022 -31115 -0,40
061 07813204 -29933 -0,38
062 07814189 -28948 -0,37
063 07812212 -30925 -0,39
064 07808881 -34256 -0,44
065 07812645 -30492 -0,39
066 07811402 -31735 -0,40
067 07814990 -28147 -0,36
068 07812811 -30326 -0,39
069 07815685 -27452 -0,35
070 07811079 -32058 -0,41
071 07817048 -26089 -0,33
072 07811274 -31863 -0,41
073 07813548 -29589 -0,38
074 07808935 -34202 -0,44
075 07810930 -32207 -0,41
076 07814384 -28753 -0,37
077 07808679 -34458 -0,44
078 07812986 -30151 -0,38
079 07813453 -29684 -0,38
080 07812955 -30182 -0,38
081 07813907 -29230 -0,37
082 07814802 -28335 -0,36
083 07811507 -31630 -0,40
084 07810017 -33120 -0,42
085 07811109 -32028 -0,41
086 07818876 -24261 -0,31
087 07814929 -28208 -0,36
088 07811212 -31925 -0,41
089 07813034 -30103 -0,38
090 07804916 -38221 -0,49
091 07812236 -30901 -0,39
092 07811387 -31750 -0,40
093 07811809 -31328 -0,40
094 07810585 -32552 -0,42
095 07814285 -28852 -0,37
096 07814075 -29062 -0,37
097 07807357 -35780 -0,46
098 07809278 -33859 -0,43
099 07812068 -31069 -0,40
100 07817680 -25457 -0,32
101 07816379 -26758 -0,34
102 07810295 -32842 -0,42
103 07806809 -36328 -0,46
104 07810986 -32151 -0,41
105 07815728 -27409 -0,35
106 07811932 -31205 -0,40
107 07808286 -34851 -0,44
108 07808231 -34906 -0,45
109 07814607 -28530 -0,36
110 07817007 -26130 -0,33
111 07812507 -30630 -0,39
112 07813549 -29588 -0,38
113 07811600 -31537 -0,40
114 07812951 -30186 -0,38
115 07815045 -28092 -0,36
116 07818553 -24584 -0,31
117 07807097 -36040 -0,46
118 07816194 -26943 -0,34
119 07813458 -29679 -0,38
120 07813533 -29604 -0,38
121 07807017 -36120 -0,46
122 07814173 -28964 -0,37
123 07815135 -28002 -0,36
124 07809412 -33725 -0,43
125 07811343 -31794 -0,41
126 07815887 -27250 -0,35
127 07812574 -30563 -0,39
128 07812997 -30140 -0,38
129 07809417 -33720 -0,43
130 07811667 -31470 -0,40
131 07814854 -28283 -0,36
132 07808924 -34213 -0,44
133 07815444 -27693 -0,35
134 07810990 -32147 -0,41
135 07810764 -32373 -0,41
136 07809610 -33527 -0,43
137 07811971 -31166 -0,40
138 07814018 -29119 -0,37
139 07811799 -31338 -0,40
140 07814179 -28958 -0,37
141 07811249 -31888 -0,41
142 07809939 -33198 -0,42
143 07808559 -34578 -0,44
144 07812129 -31008 -0,40
145 07816615 -26522 -0,34
146 07814718 -28419 -0,36
147 07810840 -32297 -0,41
148 07813156 -29981 -0,38
149 07811896 -31241 -0,40
150 07812873 -30264 -0,39
151 07815567 -27570 -0,35
152 07809732 -33405 -0,43
153 07806852 -36285 -0,46
154 07812299 -30838 -0,39
155 07815911 -27226 -0,35
156 07815599 -27538 -0,35
157 07811493 -31644 -0,40
158 07811476 -31661 -0,40
159 07816500 -26637 -0,34
160 07817239 -25898 -0,33
161 07812398 -30739 -0,39
162 07812513 -30624 -0,39
163 07817611 -25526 -0,33
164 07812411 -30726 -0,39
165 07809879 -33258 -0,42
166 07813575 -29562 -0,38
167 07813596 -29541 -0,38
168 07811003 -32134 -0,41
169 07814074 -29063 -0,37
170 07813784 -29353 -0,37
171 07810592 -32545 -0,41
172 07810882 -32255 -0,41
173 07809529 -33608 -0,43
174 07813139 -29998 -0,38
175 07814643 -28494 -0,36
176 07812761 -30376 -0,39
177 07809965 -33172 -0,42
178 07808130 -35007 -0,45
179 07807488 -35649 -0,45
180 07814433 -28704 -0,37
181 07814626 -28511 -0,36
182 07808936 -34201 -0,44
183 07812762 -30375 -0,39
184 07814267 -28870 -0,37
185 07814419 -28718 -0,37
186 07816096 -27041 -0,34
187 07810660 -32477 -0,41
188 07814438 -28699 -0,37
189 07808324 -34813 -0,44
190 07813445 -29692 -0,38
191 07809653 -33484 -0,43
192 07809146 -33991 -0,43
193 07810102 -33035 -0,42
194 07814605 -28532 -0,36
195 07809554 -33583 -0,43
196 07807564 -35573 -0,45
197 07811463 -31674 -0,40
198 07818195 -24942 -0,32
199 07812416 -30721 -0,39
200 07812923 -30214 -0,39
201 07817772 -25365 -0,32
202 07811447 -31690 -0,40
203 07812453 -30684 -0,39
204 07802922 -40215 -0,51
205 07814389 -28748 -0,37
206 07809801 -33336 -0,43
207 07817022 -26115 -0,33
208 07818214 -24923 -0,32
209 07808522 -34615 -0,44
210 07809767 -33370 -0,43
211 07817321 -25816 -0,33
212 07812845 -30292 -0,39
213 07813065 -30072 -0,38
214 07816722 -26415 -0,34
215 07813604 -29533 -0,38
216 07810457 -32680 -0,42
217 07809932 -33205 -0,42
218 07807854 -35283 -0,45
219 07814284 -28853 -0,37
220 07810929 -32208 -0,41
221 07808898 -34239 -0,44
222 07811696 -31441 -0,40
223 07815224 -27913 -0,36
224 07812093 -31044 -0,40
225 07815248 -27889 -0,36
226 07814966 -28171 -0,36
227 07813906 -29231 -0,37
228 07812929 -30208 -0,39
229 07812278 -30859 -0,39
230 07814123 -29014 -0,37
231 07816260 -26877 -0,34
232 07813003 -30134 -0,38
233 07814758 -28379 -0,36
234 07813691 -29446 -0,38
235 07811716 -31421 -0,40
236 07815428 -27709 -0,35
237 07812436 -30701 -0,39
238 07811640 -31497 -0,40
239 07814466 -28671 -0,37
240 07813669 -29468 -0,38
241 07813566 -29571 -0,38
242 07811725 -31412 -0,40
243 07809985 -33152 -0,42
244 07808256 -34881 -0,44
245 07814264 -28873 -0,37
246 07814002 -29135 -0,37
247 07818109 -25028 -0,32
248 07809138 -33999 -0,43
249 07810292 -32845 -0,42
250 07814299 -28838 -0,37
251 07806516 -36621 -0,47
252 07815049 -28088 -0,36
253 07814191 -28946 -0,37
254 07816040 -27097 -0,35
255 07814702 -28435 -0,36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment