-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRndVal.cs
More file actions
52 lines (39 loc) · 1 KB
/
RndVal.cs
File metadata and controls
52 lines (39 loc) · 1 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
public class RndVal {
private int[] list;
private int offset;
private int shuffleCount;
private int totalCount;
private int lastIndex;
public int LastValue {
get => lastIndex + offset;
}
public RndVal(int min, int max, int shuffleCount) {
if (min > max)
(min, max) = (max, min);
else if (min == max)
throw new System.ArgumentOutOfRangeException();
this.shuffleCount = shuffleCount;
offset = min;
list = new int[Mathf.Abs(max - min)];
Next();
}
public int Next() {
if (totalCount == shuffleCount * list.Length)
ResetList();
int index;
do {
index = Mathf.Clamp(Mathf.RoundToInt(Random.value * list.Length - 1), 0, list.Length - 1);
} while (list[index] == shuffleCount || index == lastIndex);
totalCount++;
list[index]++;
lastIndex = index;
return index + offset;
}
private void ResetList() {
totalCount = 0;
for (int i = 0; i < list.Length; i++)
list[i] = 0;
}
public static implicit operator int(RndVal rnd) => rnd.LastValue;
}