Skip to content

Commit 01f602c

Browse files
committed
feat: Add random with size
1 parent 98e043d commit 01f602c

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Random.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,24 +223,40 @@ private static float FindDecimalInclude(float num)
223223
}
224224

225225
/// <summary>
226-
/// Choose one object from the list.
226+
/// Choose object(s) from the collections.
227227
/// </summary>
228-
/// <typeparam name="T"> Type of the object. </typeparam>
229-
/// <param name="inArray"> The list or array to choose from. </param>
230-
/// <returns> The chosen object from the list or array. </returns>
231228
public static T ChooseOneE<T>(params T[] args) // Ellipsis
232229
{
233230
return ChooseOne(args);
234231
}
235232
public static T ChooseOne<T>(ICollection<T> lst)
236233
{
237-
if (lst.Count == 0)
238-
return default(T);
234+
if (lst == null || lst.Count == 0)
235+
return default;
239236

240237
int index = Range(0, lst.Count);
241238

242239
return lst.ElementAt(index);
243240
}
241+
public static List<T> Choose<T>(int size, ICollection<T> lst)
242+
{
243+
List<T> chosen = new();
244+
245+
JCS_Loop.Times(size, () =>
246+
{
247+
chosen.Add(ChooseOne(lst));
248+
});
249+
250+
return chosen;
251+
}
252+
253+
/// <summary>
254+
/// Choose object(s) from the collection.
255+
/// </summary>
256+
public static KeyValuePair<T, K> ChooseOneE<T, K>(params KeyValuePair<T, K>[] args) // Ellipsis
257+
{
258+
return ChooseOne(args);
259+
}
244260
public static KeyValuePair<T, K> ChooseOne<T, K>(ICollection<KeyValuePair<T, K>> dict)
245261
{
246262
if (dict == null || dict.Count == 0)
@@ -250,6 +266,17 @@ public static KeyValuePair<T, K> ChooseOne<T, K>(ICollection<KeyValuePair<T, K>>
250266

251267
return dict.ElementAt(index);
252268
}
269+
public static List<KeyValuePair<T, K>> Choose<T, K>(int size, ICollection<KeyValuePair<T, K>> dict)
270+
{
271+
List<KeyValuePair<T, K>> chosen = new();
272+
273+
JCS_Loop.Times(size, () =>
274+
{
275+
chosen.Add(ChooseOne(dict));
276+
});
277+
278+
return chosen;
279+
}
253280

254281
/// <summary>
255282
/// Return a random enum value.

0 commit comments

Comments
 (0)