-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteGenerator.cs
73 lines (62 loc) · 2.66 KB
/
ByteGenerator.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="Byte" />. Depending on the
/// constructor used, the generator can either utilize a range of all possible
/// <see cref="Byte" /> values or a constrained, smaller range.
/// </summary>
/// <remarks>
/// The range of possible <see cref="Byte" /> values will always be contiguous.
/// </remarks>
public class ByteGenerator : IntegralGenerator<Byte> {
private static ThreadLocal<Random> random { get; } =
new ThreadLocal<Random>(() => new Random());
/// <summary>
/// Instantiates an <see cref="ByteGenerator" /> that can create
/// <see cref="Byte" /> values that range from 0 (inclusively) to
/// <see cref="Byte.MaxValue" /> (exclusively).
/// </summary>
public ByteGenerator() :
this(0, Byte.MaxValue) {}
/// <summary>
/// Instantiates an <see cref="ByteGenerator" /> that can create
/// <see cref="Byte" /> values that range from <paramref name="low" />
/// (inclusively) to <see cref="Byte.MaxValue" /> (exclusively).
/// </summary>
/// <param name="low">
/// The inclusive, lower <see cref="Byte" /> boundary for this generator.
/// </param>
public ByteGenerator(Byte low) :
this(low, Byte.MaxValue) {}
/// <summary>
/// Instantiates an <see cref="ByteGenerator" /> that can create
/// <see cref="Byte" /> values that range from <paramref name="low" />
/// (inclusively) to <paramref name="high" /> (exclusively).
/// </summary>
/// <param name="low">
/// The inclusive, lower <see cref="Byte" /> boundary for this generator.
/// </param>
/// <param name="high">
/// The exclusive, upper <see cref="Byte" /> boundary for this generator.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="low" /> is greater than or equal to
/// <paramref name="high" />.
/// </exception>
public ByteGenerator(Byte low, Byte high) :
base(low, high) {}
/// <inheritdoc />
protected override sealed Byte Next(Byte low, Byte high) {
return random.Value.NextByte(low, high);
}
/// <inheritdoc />
protected override sealed Byte SubtractOne(Byte value) {
return (Byte)(value - 1);
}
/// <inheritdoc />
protected override sealed Byte AddOne(Byte value) {
return (Byte)(value + 1);
}
}
}