using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Sarand.Net.Standard.Base.Helpers
{
//=======================================================================
public static class SacsRandomHelper
{
//-------------------------------------------------------------------
// Properties.
public static RandomNumberGenerator RandomNumberGenerator { get; } = RandomNumberGenerator.Create();
private static Random random;
public static Random Random { get { if (random == null) random = new Random(GetInt32(Int32.MaxValue)); return random; } }
public static void Fill(byte[] byteArray)
{
RandomNumberGenerator.Fill(byteArray);
//RandomNumberGenerator.GetBytes(byteArray);
}
//-------------------------------------------------------------------
///
/// Generates a random integer between 0 (inclusive) and a specified exclusive upper
/// bound using a cryptographically strong random number generator.
///
/// The exclusive upper bound of the random range.
/// A random integer between 0 (inclusive) and toExclusive (exclusive).
/// The toExclusive parameter is less than or equal to 0.
public static int GetInt32(int toExclusive)
{
return RandomNumberGenerator.GetInt32(toExclusive);
//return Random.Next(toExclusive);
}
//-------------------------------------------------------------------
///
/// Generates a random integer between a specified inclusive lower bound and a specified
/// exclusive upper bound using a cryptographically strong random number generator.
///
/// The inclusive lower bound of the random range.
/// The exclusive upper bound of the random range.
/// A random integer between fromInclusive (inclusive) and toExclusive (exclusive).
/// The toExclusive parameter is less than or equal to the fromInclusive parameter.
public static int GetInt32(int fromInclusive, int toExclusive)
{
return RandomNumberGenerator.GetInt32(fromInclusive, toExclusive);
//return Random.Next(fromInclusive, toExclusive);
}
//-------------------------------------------------------------------
}
//=======================================================================
}