-
Notifications
You must be signed in to change notification settings - Fork 1
/
XorShift128.cs
118 lines (105 loc) · 3.61 KB
/
XorShift128.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Copyright (c) 2018-2024 Nuno Fachada and contributors
* Distributed under the MIT License (See accompanying file LICENSE or copy
* at http://opensource.org/licenses/MIT) */
using System;
namespace LibGameAI.PRNG
{
/// <summary>
/// A 128-bit implementation of a XorShift pseudo-random number generator.
/// </summary>
/// <remarks>
/// Useful links:
/// <list type="bullet">
/// <item><description>
/// <seealso href="http://en.wikipedia.org/wiki/Xorshift">
/// Xorshift - Wikipedia
/// </seealso>
/// </description></item>
/// <item><description>
/// <seealso href="https://github.com/fakenmc/cl_ops/blob/master/src/cl_ops/rng/clo_rng_xorshift128.cl">
/// Implementation of this particular XorShift in OpenCL
/// </seealso>
/// </description></item>
/// <item><description>
/// <seealso href="https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/Random.cs">
/// .NET Core implementation of the Random class
/// </seealso>
/// </description></item>
/// <item><description>
/// <seealso href="https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/random.cs">
/// .NET Framework implementation of the Random class
/// </seealso>
/// </description></item>
/// <item><description>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/api/system.random?view=net-5.0#notes-to-inheritors">
/// Documentation on how to extend the Random class
/// </seealso>
/// </description></item>
/// </list>
/// </remarks>
public class XorShift128 : Random
{
private uint x, y, z, w;
public XorShift128() : this(System.DateTime.Now.Ticks)
{
}
public XorShift128(int seed) : this((long)seed << 32 | (long)seed)
{
}
public XorShift128(long seed)
{
if (seed == 0) seed = (long)-1;
x = unchecked((uint)(0xFFFFFFFF & seed));
y = unchecked((uint)(0xFFFFFFFF & (seed >> 16)));
z = unchecked((uint)(0xFFFFFFFF & (seed >> 32)));
w = unchecked((uint)(0xFFFFFFFF & (seed >> 48)));
}
private void UpdateState()
{
uint t = x ^ (x << 11);
x = y;
y = z;
z = w;
w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}
// Implements the random number generation algorithm
protected override double Sample()
{
UpdateState();
return (double)(((ulong)z << 32) | (ulong)w) / ulong.MaxValue;
}
public override int Next()
{
UpdateState();
return unchecked((int)w);
}
public override int Next(int minValue, int maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException(
$"{nameof(minValue)} cannot be larger than ${nameof(maxValue)}");
}
long range = (long)maxValue - minValue;
if (range <= int.MaxValue)
{
return (int)(Sample() * range) + minValue;
}
else
{
return (int)(Sample() * range / 2.0 + Sample() * range / 2.0)
+ minValue;
}
}
public override void NextBytes(byte[] buffer)
{
if (buffer == null)
throw new ArgumentNullException(
$"{nameof(buffer)} cannot be null");
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)Next();
}
}
}
}