I was reading about Random.Next() that for "cryptographically secure random number suitable for creating a random password" MSDN suggests RNGCryptoServiceProvider Class
What the speed penality? There is some fastest way to get true random numbers?
EDIT: With Random.Next() I get a new random number. And with...
byte[] randomNumber = new byte[1];
RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
Gen.GetBytes(randomNumber);
int rand = Convert.ToInt32(randomNumber[0]);
I get a "cryptographically secure random number" I want know if the above code is fast compared with "Random.Next()" and if there is some fast way to get same results, ok?
AFAIK a true random generator will never be implemented in C#. That can only be done with the help of hardware?
The rule of thumb when it comes to security and cryptography stuff:
Never write your own.
Go with the standard way of doing it and avoid dangerous optimizations.
Edit to address the updated question:
Use
Random.Next
when you need statistically random numbers not used in security sensitive code andRNGCryptoServiceProvider
in security sensitive code. It's not as fast asRandom.Next
but it has acceptable performance. You should benchmark to see the actual difference. It usually doesn't make sense to sacrifice security for performance.