-
Notifications
You must be signed in to change notification settings - Fork 0
/
UInt32Generator.cs
73 lines (62 loc) · 2.7 KB
/
UInt32Generator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Threading;
namespace Peddler {
/// <summary>
/// A generator for numbers of type <see cref="UInt32" />. Depending on the
/// constructor used, the generator can either utilize a range of all possible
/// <see cref="UInt32" /> values or a constrained, smaller range.
/// </summary>
/// <remarks>
/// The range of possible <see cref="UInt32" /> values will always be contiguous.
/// </remarks>
public class UInt32Generator : IntegralGenerator<UInt32> {
private static ThreadLocal<Random> random { get; } =
new ThreadLocal<Random>(() => new Random());
/// <summary>
/// Instantiates an <see cref="UInt32Generator" /> that can create
/// <see cref="UInt32" /> values that range from 0 (inclusively) to
/// <see cref="UInt32.MaxValue" /> (exclusively).
/// </summary>
public UInt32Generator() :
this(0, UInt32.MaxValue) {}
/// <summary>
/// Instantiates an <see cref="UInt32Generator" /> that can create
/// <see cref="UInt32" /> values that range from <paramref name="low" />
/// (inclusively) to <see cref="UInt32.MaxValue" /> (exclusively).
/// </summary>
/// <param name="low">
/// The inclusive, lower <see cref="UInt32" /> boundary for this generator.
/// </param>
public UInt32Generator(UInt32 low) :
this(low, UInt32.MaxValue) {}
/// <summary>
/// Instantiates an <see cref="UInt32Generator" /> that can create
/// <see cref="UInt32" /> values that range from <paramref name="low" />
/// (inclusively) to <paramref name="high" /> (exclusively).
/// </summary>
/// <param name="low">
/// The inclusive, lower <see cref="UInt32" /> boundary for this generator.
/// </param>
/// <param name="high">
/// The exclusive, upper <see cref="UInt32" /> boundary for this generator.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="low" /> is greater than or equal to
/// <paramref name="high" />.
/// </exception>
public UInt32Generator(UInt32 low, UInt32 high) :
base(low, high) {}
/// <inheritdoc />
protected override sealed UInt32 Next(UInt32 low, UInt32 high) {
return random.Value.NextUInt32(low, high);
}
/// <inheritdoc />
protected override sealed UInt32 SubtractOne(UInt32 value) {
return value - 1;
}
/// <inheritdoc />
protected override sealed UInt32 AddOne(UInt32 value) {
return value + 1;
}
}
}