I am using C# RNGCryptoServiceProvider inside a using block
byte[] values = new byte[ammountToCreate];
using (RNGCryptoServiceProvider randomizer = new RNGCryptoServiceProvider())
{
randomizer.GetBytes(values);
}
The result values are then normalized to interval [0:15], therefore no modulo bias should affect the output.
Normalization is done in this way:
byte n = (byte)(this.maxRandomValue + 1);
// loop all element in the input value, an apply modulo.
for (int i = 0; i < values.Length; i++)
{
values[i] = (byte)(values[i] % n);
}
Where maxRandomValue is equal to 15.
However, from the first test, the output seems to be not uniform. I have generated 2916 numbers, and this is the values distribution:
+-------+--------------+
| Value | % On Total |
+-------+--------------+
| 0 | 6.52% |
| 1 | 5.90% |
| 2 | 6.10% |
| 3 | 6.34% |
| 4 | 5.73% |
| 5 | 6.89% |
| 6 | 5.49% |
| 7 | 6.86% |
| 8 | 5.66% |
| 9 | 5.97% |
| 10 | 6.58% |
| 11 | 6.04% |
| 12 | 6.48% |
| 13 | 5.97% |
| 14 | 7.17% |
| 15 | 6.31% |
+-------+--------------+
As you can see: 6 --> 5.49% of generated numbers 14 --> 7.17% of generated numbers
My fear is that maybe I have generated just few numbers and with bigger volumes the distribution becomes uniform. Or, RNGCryptoServiceProvider inside a using is not working as expected.
Do you have any idea?