RNGCryptoServiceProvider - Random Number Review

  • Thread starter Thread starter TechPlanet
  • Start date Start date
T

TechPlanet

Guest
While looking for best attempts at generating truly random numbers, I stumbled upon this code example.

Looking for opinions on this snippet.

using System;
using System.Security.Cryptography;

private static int NextInt(int min, int max)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buffer = new byte[4];

rng.GetBytes(buffer);
int result = BitConverter.ToInt32(buffer, 0);

return new Random(result).Next(min, max);
}

Would this be preferred over using a tick count seed such as:

Random rand = new Random(Environment.TickCount);
rand.Next(min, max);
Note:

I am not looking for third party random data providers such as Random.org, as such a dependency is not realistic to the application.

Continue reading...
 
Back
Top