-
Notifications
You must be signed in to change notification settings - Fork 0
/
SRandom.cs
59 lines (47 loc) · 1.33 KB
/
SRandom.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
using System;
public class SRandom
{
public static int count = 0;
ulong randSeed = 1;
public SRandom(uint seed)
{
randSeed = seed;
}
public uint Next()
{
randSeed = randSeed * 1103515245 + 12345;
return (uint)(randSeed / 65536);
}
// range:[0 ~(max-1)]
public uint Next(uint max)
{
return Next() % max;
}
// range:[min~(max-1)]
public uint Range(uint min, uint max)
{
if (min > max)
throw new ArgumentOutOfRangeException("minValue", string.Format("'{0}' cannot be greater than {1}.", min, max));
uint num = max - min;
return Next(num) + min;
}
public int Next(int max)
{
return (int)(Next() % max);
}
public int Range(int min, int max)
{
count++;
if (min > max)
throw new ArgumentOutOfRangeException("minValue", string.Format("'{0}' cannot be greater than {1}.", min, max));
int num = max - min;
return Next(num) + min;
}
public Fix64 Range(Fix64 min, Fix64 max)
{
if (min > max)
throw new ArgumentOutOfRangeException("minValue", string.Format("'{0}' cannot be greater than {1}.", min, max));
uint num = (uint)(max.RawValue - min.RawValue);
return Fix64.FromRaw(Next(num) + min.RawValue);
}
}