Created
January 3, 2016 06:45
-
-
Save divide-by-zero/d457c8116d88428c6318 to your computer and use it in GitHub Desktop.
配列(IEnumerable)の中からランダムで一つ返却するLinq拡張 ref: http://qiita.com/divideby_zero/items/3a5f92a9208f87204cce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class LinqExtensions | |
| { | |
| public static T RandomAt<T>(this IEnumerable<T> ie) | |
| { | |
| if (ie.Any() == false) return default(T); | |
| return ie.ElementAt(Random.Range(0, ie.Count())); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void Start() | |
| { | |
| var data = new []{ 1, 2, 3, 4, 5, 10, 20, 31, 32, 33, 34, 35 }; | |
| var r1 = data.RandomAt(); | |
| Debug.Log("r1:" + r1); | |
| var r2 = data.Where(num => num % 2 == 0).RandomAt(); | |
| Debug.Log("r2:" + r2); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment