From 6bf783e67420d037773e981e745be6ab1fc5819b Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 12 Jul 2013 20:20:48 +1200 Subject: [PATCH] Add IEnumerable.RandomOrDefault extension. --- OpenRA.FileFormats/Exts.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index af2e5d98ef..30a629c59d 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -110,6 +110,14 @@ namespace OpenRA return xs[r.Next(xs.Length)]; } + public static T RandomOrDefault(this IEnumerable ts, Thirdparty.Random r) + { + if (!ts.Any()) + return default(T); + + return ts.Random(r); + } + public static float Product(this IEnumerable xs) { return xs.Aggregate(1f, (a, x) => a * x);