Skip to content

Commit 9a562a5

Browse files
committed
Added GetRandomItem
1 parent 6e1fa9a commit 9a562a5

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [1.0.4] - 2022-01-11
8+
9+
### Added
10+
- Added `MathUtilities.GetRandomItem()` method to choose a random item from the given array or list.

Runtime/Scripts/MathUtilities.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ public static bool RandomChance(float numerator, float denominator = 100f)
106106
return false;
107107
}
108108

109+
/// <summary>
110+
/// returns a random item from the given array.
111+
/// </summary>
112+
public static T GetRandomItem<T>(T[] array)
113+
{
114+
return array[Random.Range(0, array.Length)];
115+
}
116+
117+
/// <summary>
118+
/// returns a random item from the given list.
119+
/// </summary>
120+
public static T GetRandomItem<T>(List<T> list)
121+
{
122+
return GetRandomItem(list.ToArray());
123+
}
124+
109125
public static Vector2 RandomVector2()
110126
{
111127
return new Vector2(Random.Range(-1, 1), Random.Range(-1, 1)).normalized;
@@ -270,7 +286,7 @@ public static Vector2 RotateVector2(Vector2 vector, float angle)
270286
{
271287
var cos = Mathf.Cos(angle);
272288
var sin = Mathf.Sin(angle);
273-
289+
274290
return new Vector2(
275291
cos * vector.x - sin * vector.y,
276292
sin * vector.x + cos * vector.y

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.dartcore.utilities",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"displayName": "DartCore Utilities",
55
"description": "This package contains methods to handle commonly required stuff.",
66
"unity": "2018.1",

0 commit comments

Comments
 (0)