I was reading about Random.Next() that for \"cryptographically secure random number suitable for creating a random password\" MSDN suggests RNGCryptoServiceProvider Class
The "cryptographically secure random number" generated by your example code will only ever be between 0 and 255 inclusive!
If you want to return all possible Int32
values then you should use 4 random bytes. Your code should look something like this:
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] rndBytes = new byte[4];
rng.GetBytes(rndBytes);
int rand = BitConverter.ToInt32(rndBytes, 0);
A quick benchmark on my (old-ish) machine suggests that Random.Next
is approximately 200x faster than using RNGCryptoServiceProvider
.