-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinearCongruentialRandomNumberGenerator.cs
More file actions
35 lines (26 loc) · 1.12 KB
/
LinearCongruentialRandomNumberGenerator.cs
File metadata and controls
35 lines (26 loc) · 1.12 KB
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
namespace AlgorithmsAndDataStructures.Algorithms.PseudorandomNumberGenerators;
public class LinearCongruentialRandomNumberGenerator
{
// This value was proven to be a good candidate for a seed.
private const long Seed = 4294967296;
// This value was proven to be a good candidate for a multiplier.
private const int Multiplier = 32310901;
// This value has no particular recommendations, except for an oddity of this value.
private const int Increment = 321777;
// This value has no particular recommendations, the only recommendation is that it should be a power of two. This one is Math.Pow(2, 26).
private const int Modulos = 67108864;
private long lastGeneratedValue;
public LinearCongruentialRandomNumberGenerator()
{
lastGeneratedValue = GenerateInternal(Seed);
}
public long Generate()
{
lastGeneratedValue = GenerateInternal(lastGeneratedValue);
return lastGeneratedValue;
}
private static long GenerateInternal(long previous)
{
return (Multiplier * previous + Increment) % Modulos;
}
}