public static Decimal NextDecimal() {
return new Decimal( new[] { NextInt32(), NextInt32(), NextInt32(), NextInt32() } );
}
public static int NextInt32() {
unchecked {
var firstBits = Instance.Next( 0, 1 << 4 ) << 28;
var lastBits = Instance.Next( 0, 1 << 28 );
return firstBits | lastBits;
}
}
/// <summary>
/// A thread-local (threadsafe) <see cref="Random" />.
/// </summary>
public static Random Instance { get { return ThreadSafeRandom.Value; } }
private static readonly ThreadLocal<SHA256Managed> Crypts = new ThreadLocal<SHA256Managed>( valueFactory: () => new SHA256Managed(), trackAllValues: false );
/// <summary>
/// Provide to each thread its own <see cref="Random" /> with a random seed.
/// </summary>
public static readonly ThreadLocal<Random> ThreadSafeRandom = new ThreadLocal<Random>( valueFactory: () => {
var hash = Crypts.Value.ComputeHash( Guid.NewGuid().ToByteArray() );
var newSeed = BitConverter.ToInt32( hash, 0 );
return new Random( newSeed );
}, trackAllValues: false );
No comments:
Post a Comment