Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Source/Bogus/Randomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Randomizer
public Randomizer()
{
this.localSeed = Seed;
this.localLocker = Locker;
}

/// <summary>
Expand All @@ -39,12 +40,15 @@ public Randomizer()
public Randomizer(int localSeed)
{
this.localSeed = new Random(localSeed);
this.localLocker = new Lazy<object>(() => new object(), LazyThreadSafetyMode.ExecutionAndPublication);
}

/// <summary>
/// The pseudo-random number generator that is used for all random number generation in this instance.
/// </summary>
protected Random localSeed;

internal Lazy<object> localLocker;

/// <summary>
/// Get an int from 0 to max.
Expand Down Expand Up @@ -82,7 +86,7 @@ public int[] Digits(int count, int minDigit = 0, int maxDigit = 9)
public int Number(int min = 0, int max = 1)
{
//lock any seed access, for thread safety.
lock( Locker.Value )
lock( localLocker.Value )
{
// Adjust the range as needed to make max inclusive. The Random.Next function uses exclusive upper bounds.

Expand Down Expand Up @@ -178,7 +182,7 @@ public int Odd(int min = 0, int max = 1)
public double Double(double min = 0.0d, double max = 1.0d)
{
//lock any seed access, for thread safety.
lock( Locker.Value )
lock( localLocker.Value )
{
if( min == 0.0d && max == 1.0d )
{
Expand Down Expand Up @@ -227,7 +231,7 @@ public byte Byte(byte min = byte.MinValue, byte max = byte.MaxValue)
public byte[] Bytes(int count)
{
var arr = new byte[count];
lock( Locker.Value )
lock( localLocker.Value )
{
localSeed.NextBytes(arr);
}
Expand Down Expand Up @@ -720,7 +724,7 @@ public IEnumerable<T> Shuffle<T>(IEnumerable<T> source)
{
int j;
//lock any seed access, for thread safety.
lock( Locker.Value )
lock( localLocker.Value )
{
j = this.localSeed.Next(i, buffer.Count);
}
Expand Down