-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandomGen.cs
62 lines (51 loc) · 1.71 KB
/
RandomGen.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
using UnityEngine;
using System.Security.Cryptography;
using System;
public static class RandomGen
{
private static RNGCryptoServiceProvider _global =
new RNGCryptoServiceProvider();
[ThreadStatic]
private static System.Random _local;
public static bool FlipACoin()
{
System.Random inst = _local;
if (inst == null)
{
byte[] buffer = new byte[4];
_global.GetBytes(buffer);
_local = inst = new System.Random(
BitConverter.ToInt32(buffer, 0));
}
return inst.Next() % 2 == 0;
}
public static float Range01()
{
System.Random inst = _local;
if (inst == null)
{
byte[] buffer = new byte[4];
_global.GetBytes(buffer);
_local = inst = new System.Random(
BitConverter.ToInt32(buffer, 0));
}
return inst.Next(0, 1000000) * 0.000001f;
}
// public static int NextValidRandomPatchAmountFromTGOSRange()
// {
// return
//RandomGen.Next(TownGlobalObjectService.PatchCap, TownGlobalObjectService.PatchFloor);
// }
public static int Next(int Ceil = int.MaxValue, int Floor = int.MinValue)
{
System.Random inst = _local;
if (inst == null)
{
byte[] buffer = new byte[4];
_global.GetBytes(buffer);
_local = inst = new System.Random(
BitConverter.ToInt32(buffer, 0));
}
return Mathf.Max(Floor, inst.Next() % Mathf.Max(1, Ceil));
}
}