-
Notifications
You must be signed in to change notification settings - Fork 1
/
SeededRng.cs
43 lines (35 loc) · 1.05 KB
/
SeededRng.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
using Randomizer.Config;
namespace Randomizer;
public static class SeededRng
{
// Supposedly not good to use .getHashCode() by itself, but don't think it matters for this use-case
// https://stackoverflow.com/a/9084760
public static int Seed = Guid.NewGuid().GetHashCode();
private static Random? _rng;
public static Random? Rng
{
get => _rng ?? new(Seed);
set => _rng = value;
}
public static void ResetRng() => _rng = new(Seed);
public static void UpdateSeed(int seed)
{
if (!RandomizerConfig.Instance.UseConfigSeed)
{
Seed = seed;
ResetRng();
}
}
public static List<T> FisherYatesShuffle<T>(List<T> originalList)
{
List<T> list = new(originalList);
if (Rng == null) return originalList;
var n = list.Count();
for (int i = 0; i < (n - 1); i++)
{
var swapIdx = i + SeededRng.Rng.Next(n - i);
(list[i], list[swapIdx]) = (list[swapIdx], list[i]);
}
return list;
}
}