While looking for best attempts at generating truly random numbers, I stumbled upon this code example.
Looking for opinions on this snippet.
<
I felt like this could be a lot simpler and achieved in fewer lines of code so I'll throw my effort in - this will always return a positive number between 0 and 1:
public double Random()
{
using var csp = new RNGCryptoServiceProvider();
byte[] b = new byte[8];
csp.GetBytes(b);
var lng = BitConverter.ToInt64(b, 0);
var dbl = (double)(lng < 0 ? ~lng : lng);
// Convert to a random number between 0 and 1
return dbl / long.MaxValue;
}
Instead of creating a new RNGCryptoServiceProvider each time, a simple static field would do fine.
To return a random positive integer between two numbers this works fine - though you need to check min is less than max, and that both are positive:
public long RandomInt64(long min = 0, long max = long.MaxValue)
{
// Check arguments
if (min >= max)
{
throw new ArgumentOutOfRangeException(nameof(min), min, "Minimium value must be less than the maximum value.");
}
if (min < 0)
{
throw new ArgumentException("Minimum value must be at least 0.", nameof(min));
}
// Get the range between the specified minimum and maximum values
var range = max - min;
// Now add a random amount of the range to the minimum value - it will never exceed maximum value
var add = Math.Round(range * Random());
return (long)(min + add);
}