-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomUtility.cs
More file actions
270 lines (240 loc) · 7.21 KB
/
RandomUtility.cs
File metadata and controls
270 lines (240 loc) · 7.21 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnityEssentials
{
/// <summary>
/// Helper class for random operations.
/// </summary>
public static class RandomUtility
{
/// <summary>
/// Generates a random boolean using the given probability.
/// </summary>
public static bool Probability(float prob)
{
return Random.value <= prob;
}
/// <summary>
/// Returns a random point within the given bounds.
/// </summary>
public static Vector2 RandomPoint2D(Vector2 min, Vector2 max)
{
return new Vector2(Random.Range(min.x, max.x), Random.Range(min.y, max.y));
}
/// <summary>
/// Returns a random point within the given bounds.
/// </summary>
public static Vector2 RandomPoint2D(float min, float max)
{
return new Vector2(Random.Range(min, max), Random.Range(min, max));
}
/// <summary>
/// Returns a random point within the given bounds.
/// </summary>
public static Vector3 RandomPoint3D(Vector3 min, Vector3 max)
{
return new Vector3(Random.Range(min.x, max.x), Random.Range(min.y, max.y), Random.Range(min.z, max.z));
}
/// <summary>
/// Returns a random point within the given bounds.
/// </summary>
public static Vector3 RandomPoint3D(float min, float max)
{
return new Vector3(Random.Range(min, max), Random.Range(min, max), Random.Range(min, max));
}
/// <summary>
/// Returns a random point within the given bounds.
/// </summary>
public static Vector4 RandomPoint4D(Vector4 min, Vector4 max)
{
return new Vector4(Random.Range(min.x, max.x), Random.Range(min.y, max.y), Random.Range(min.z, max.z), Random.Range(min.w, max.w));
}
/// <summary>
/// Returns a random point within the given bounds.
/// </summary>
public static Vector4 RandomPoint4D(float min, float max)
{
return new Vector4(Random.Range(min, max), Random.Range(min, max), Random.Range(min, max), Random.Range(min, max));
}
/// <summary>
/// Returns a random integer between min (inclusive) and max (exclusive), excluding the given integers.
/// </summary>
public static int RangeExcluding(int min, int max, params int[] exclude)
{
if(max - min <= 0) return -1;
// Build valid range without LINQ to avoid allocations
int rangeSize = max - min;
int validCount = rangeSize;
// Count how many values are excluded
for(int i = 0; i < exclude.Length; i++)
{
if(exclude[i] >= min && exclude[i] < max)
{
validCount--;
}
}
if(validCount <= 0) return -1;
// Pick a random index in the valid range
int pick = Random.Range(0, validCount);
int current = min;
// Skip excluded values
for(int offset = 0; offset <= pick; offset++)
{
while(System.Array.IndexOf(exclude, current) >= 0)
{
current++;
}
if(offset < pick) current++;
}
return current;
}
/// <summary>
/// Picks a random item from the given array.
/// </summary>
/// <returns>A random item from the array.</returns>
public static T PickRandom<T>(params T[] array)
{
if(array == null || array.Length == 0)
{
throw new System.ArgumentException("Array cannot be null or empty");
}
return array[Random.Range(0, array.Length)];
}
/// <summary>
/// Picks a random item from the given list.
/// </summary>
/// <returns>A random item from the list.</returns>
public static T PickRandom<T>(List<T> list)
{
if(list == null || list.Count == 0)
{
throw new System.ArgumentException("List cannot be null or empty");
}
return list[Random.Range(0, list.Count)];
}
/// <summary>
/// Picks a random item from the given array, excluding items at the given indices.
/// </summary>
/// <returns>A random item from the array.</returns>
public static T PickRandomExcluding<T>(T[] array, params int[] excludeIndices)
{
int index = PickRandomIndexExcluding(array.Length, excludeIndices);
if(index >= 0) return array[index];
else return default;
}
/// <summary>
/// Picks a random item from the given list, excluding items at the given indices.
/// </summary>
/// <returns>A random item from the list.</returns>
public static T PickRandomExcluding<T>(List<T> list, params int[] excludeIndices)
{
int index = PickRandomIndexExcluding(list.Count, excludeIndices);
if(index >= 0) return list[index];
else return default;
}
/// <summary>
/// Picks a random index between zero (inclusive) and the given length (exclusive), excluding the given indices.
/// </summary>
/// <returns>A random item from the array.</returns>
public static int PickRandomIndexExcluding(int length, params int[] excludeIndices)
{
// Calculate valid count without LINQ to avoid allocations
int validCount = length;
for(int i = 0; i < excludeIndices.Length; i++)
{
if(excludeIndices[i] >= 0 && excludeIndices[i] < length)
{
validCount--;
}
}
if(validCount <= 0) return -1;
// Pick a random index in the valid range
int pick = Random.Range(0, validCount);
int current = 0;
// Skip excluded indices
for(int offset = 0; offset <= pick; offset++)
{
while(System.Array.IndexOf(excludeIndices, current) >= 0)
{
current++;
}
if(offset < pick) current++;
}
return current;
}
/// <summary>
/// Picks a random item and removes it from the list.
/// </summary>
public static T TakeRandomItem<T>(List<T> list)
{
if(list == null || list.Count == 0)
{
throw new System.ArgumentException("List cannot be null or empty");
}
int i = Random.Range(0, list.Count);
var item = list[i];
list.RemoveAt(i);
return item;
}
/// <summary>
/// Picks a random weighted index using the given weighted array.
/// </summary>
/// <param name="weights">A weighted array. Higher values have a greater chance of being picked.</param>
/// <returns>The picked item's index.</returns>
public static int PickRandomWeighted(float[] weights)
{
if(weights == null || weights.Length == 0)
{
throw new System.ArgumentException("Weights array cannot be null or empty");
}
float total = 0;
foreach(var w in weights) total += w;
if(total <= 0)
{
throw new System.ArgumentException("Total weight must be greater than zero");
}
float pick = Random.value * total;
for(int i = 0; i < weights.Length; i++)
{
pick -= weights[i];
if(pick <= 0)
{
return i;
}
}
return weights.Length - 1;
}
/// <summary>
/// Returns a shuffled version of the given array using the Fisher-Yates algorithm.
/// </summary>
public static T[] Shuffle<T>(T[] array)
{
T[] shuffled = new T[array.Length];
System.Array.Copy(array, shuffled, array.Length);
// Fisher-Yates shuffle algorithm
for(int i = shuffled.Length - 1; i > 0; i--)
{
int j = Random.Range(0, i + 1);
T temp = shuffled[i];
shuffled[i] = shuffled[j];
shuffled[j] = temp;
}
return shuffled;
}
/// <summary>
/// Randomly shuffles the given list using the Fisher-Yates algorithm.
/// </summary>
public static void Shuffle<T>(List<T> list)
{
// Fisher-Yates shuffle algorithm - in-place
for(int i = list.Count - 1; i > 0; i--)
{
int j = Random.Range(0, i + 1);
T temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}