| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Quadarax.Foundation.Core.Value.Extensions
- {
- public static class EnumerableExt
- {
- private static Random _rand = new Random();
- private const string CS_MESSAGE_MUST_HAVE_ONE = "Collection must have at least one element.";
- private const string CS_MESSAGE_MUST_POSITIVE = "Must be positive number.";
- private const string CS_MESSAGE_MUST_SMALLER = "fromIndex must be smaller or equal to toIndex.";
- public static TElement GetRandom<TElement>(this IEnumerable<TElement> owner)
- {
- if (owner == null)
- throw new ArgumentNullException(nameof(owner));
- if (!owner.Any())
- throw new ArgumentOutOfRangeException(nameof(owner), CS_MESSAGE_MUST_HAVE_ONE);
- return GetRandom(owner, 0, owner.Count());
- }
- public static TElement GetRandom<TElement>(this IEnumerable<TElement> owner, int fromIndex, int toIndex = -1)
- {
- if (owner == null)
- throw new ArgumentNullException(nameof(owner));
- if (!owner.Any())
- throw new ArgumentOutOfRangeException(nameof(owner), CS_MESSAGE_MUST_HAVE_ONE);
- if (toIndex == -1)
- toIndex = owner.Count() - 1;
- if (fromIndex<0)
- throw new ArgumentOutOfRangeException(nameof(fromIndex), CS_MESSAGE_MUST_POSITIVE);
- if (toIndex<0)
- throw new ArgumentOutOfRangeException(nameof(fromIndex), CS_MESSAGE_MUST_POSITIVE);
- if (fromIndex>toIndex)
- throw new ArgumentOutOfRangeException(nameof(fromIndex), CS_MESSAGE_MUST_SMALLER);
- var index = _rand.Next(fromIndex, toIndex);
- return owner.Skip(index).Take(1).First();
- }
- }
- }
|