@@ -233,6 +233,17 @@ public static int RandomSelectIndex<T>(this IReadOnlyCollection<T> source, IEnum
233233 public static int RandomSelectIndex < T > ( this ICollection < T > source , IEnumerable < T > exclusion = null )
234234 => RandomSelectIndex ( source . Count , source , exclusion ) ;
235235
236+ /// <summary>
237+ /// Randomly selects an index from the source.
238+ /// Will not return indexes that are contained in the optional exclusion set.
239+ /// </summary>
240+ /// <typeparam name="T">The generic type of the source.</typeparam>
241+ /// <param name="source">The source array.</param>
242+ /// <param name="exclusion">The optional values to exclude from selection.</param>
243+ /// <returns>The index selected.</returns>
244+ public static int RandomSelectIndex < T > ( this T [ ] source , IEnumerable < T > exclusion = null )
245+ => RandomSelectIndex ( source . Length , source , exclusion ) ;
246+
236247 /// <summary>
237248 /// Randomly selects an index from the source.
238249 /// Will not return indexes that are contained in the optional exclusion set.
@@ -343,6 +354,18 @@ public static int RandomSelectIndexExcept<T>(this IReadOnlyCollection<T> source,
343354 public static int RandomSelectIndexExcept < T > ( this ICollection < T > source , T exclusion , params T [ ] others )
344355 => RandomSelectIndexExcept ( source . Count , source , exclusion , others ) ;
345356
357+ /// <summary>
358+ /// Randomly selects an index from the source.
359+ /// Will not return indexes that are contained in the optional exclusion set.
360+ /// </summary>
361+ /// <typeparam name="T">The generic type of the source.</typeparam>
362+ /// <param name="source">The source array.</param>
363+ /// <param name="exclusion">A value to exclude from selection.</param>
364+ /// <param name="others">The additional set of optional values to exclude from selection.</param>
365+ /// <returns>The index selected.</returns>
366+ public static int RandomSelectIndexExcept < T > ( this T [ ] source , T exclusion , params T [ ] others )
367+ => RandomSelectIndexExcept ( source . Length , source , exclusion , others ) ;
368+
346369 /// <summary>
347370 /// Attempts to select an index at random from the source and returns the value from it..
348371 /// Will not select indexes that are contained in the optional exclusion set.
@@ -388,7 +411,7 @@ public static bool TryRandomSelectOne<T>(
388411 /// Will not select indexes that are contained in the optional exclusion set.
389412 /// </summary>
390413 /// <typeparam name="T">The generic type of the source.</typeparam>
391- /// <param name="source">The source span .</param>
414+ /// <param name="source">The source collection .</param>
392415 /// <param name="exclusion">The optional values to exclude from selection.</param>
393416 /// <returns>The value selected.</returns>
394417 public static T RandomSelectOne < T > (
@@ -412,7 +435,7 @@ public static T RandomSelectOne<T>(
412435 /// Will not select indexes that are contained in the optional exclusion set.
413436 /// </summary>
414437 /// <typeparam name="T">The generic type of the source.</typeparam>
415- /// <param name="source">The source span .</param>
438+ /// <param name="source">The source collection .</param>
416439 /// <param name="exclusion">The optional values to exclude from selection.</param>
417440 /// <returns>The value selected.</returns>
418441 public static T RandomSelectOne < T > (
@@ -431,6 +454,28 @@ public static T RandomSelectOne<T>(
431454 : source . ElementAt ( index ) ;
432455 }
433456
457+ /// <summary>
458+ /// Selects an index at random from the source and returns the value from it.
459+ /// Will not select indexes that are contained in the optional exclusion set.
460+ /// </summary>
461+ /// <typeparam name="T">The generic type of the source.</typeparam>
462+ /// <param name="source">The source array.</param>
463+ /// <param name="exclusion">The optional values to exclude from selection.</param>
464+ /// <returns>The value selected.</returns>
465+ public static T RandomSelectOne < T > (
466+ this T [ ] source ,
467+ IEnumerable < T > exclusion = null )
468+ {
469+ if ( source . Length == 0 )
470+ throw new InvalidOperationException ( "Source collection is empty." ) ;
471+
472+ var index = RandomSelectIndex ( source , exclusion ) ;
473+ if ( index == - 1 )
474+ throw new InvalidOperationException ( "Exclusion set invalidates the source. No possible value can be selected." ) ;
475+
476+ return source [ index ] ;
477+ }
478+
434479 /// <summary>
435480 /// Attempts to select an index at random from the source and returns the value from it..
436481 /// Will not select indexes that are contained in the optional exclusion set.
@@ -487,6 +532,32 @@ public static bool TryRandomSelectOne<T>(
487532 return true ;
488533 }
489534
535+ /// <summary>
536+ /// Attempts to select an index at random from the source and returns the value from it..
537+ /// Will not select indexes that are contained in the optional exclusion set.
538+ /// </summary>
539+ /// <typeparam name="T">The generic type of the source.</typeparam>
540+ /// <param name="source">The source array.</param>
541+ /// <param name="value">The value selected.</param>
542+ /// <param name="exclusion">The optional values to exclude from selection.</param>
543+ /// <returns>True if a valid value was selected.</returns>
544+ public static bool TryRandomSelectOne < T > (
545+ this T [ ] source ,
546+ out T value ,
547+ IEnumerable < T > exclusion = null )
548+ {
549+ var index = RandomSelectIndex ( source , exclusion ) ;
550+ if ( index == - 1 )
551+ {
552+ value = default ;
553+ return false ;
554+ }
555+
556+ value = source [ index ] ;
557+
558+ return true ;
559+ }
560+
490561 /// <summary>
491562 /// Attempts to select an index at random from the source and returns the value from it..
492563 /// Will not select indexes that are contained in the optional exclusion set.
@@ -587,6 +658,33 @@ public static bool TryRandomSelectOneExcept<T>(
587658 return true ;
588659 }
589660
661+ /// <summary>
662+ /// Attempts to select an index at random from the source and returns the value from it..
663+ /// Will not select indexes that are contained in the optional exclusion set.
664+ /// </summary>
665+ /// <typeparam name="T">The generic type of the source.</typeparam>
666+ /// <param name="source">The source array.</param>
667+ /// <param name="value">The value selected.</param>
668+ /// <param name="excluding">The value to exclude from selection.</param>
669+ /// <param name="others">The additional set of optional values to exclude from selection.</param>
670+ /// <returns>True if a valid value was selected.</returns>
671+ public static bool TryRandomSelectOneExcept < T > (
672+ this T [ ] source ,
673+ out T value ,
674+ T excluding , params T [ ] others )
675+ {
676+ var index = RandomSelectIndexExcept ( source , excluding , others ) ;
677+ if ( index == - 1 )
678+ {
679+ value = default ;
680+ return false ;
681+ }
682+
683+ value = source [ index ] ;
684+
685+ return true ;
686+ }
687+
590688 /// <summary>
591689 /// Selects an index at random from the source and returns the value from it.
592690 /// Will not select indexes that are contained in the optional exclusion set.
@@ -667,6 +765,28 @@ public static T RandomSelectOneExcept<T>(
667765 throw new InvalidOperationException ( "Exclusion set invalidates the source. No possible value can be selected." ) ;
668766 }
669767
768+ /// <summary>
769+ /// Selects an index at random from the source and returns the value from it.
770+ /// Will not select indexes that are contained in the optional exclusion set.
771+ /// </summary>
772+ /// <typeparam name="T">The generic type of the source.</typeparam>
773+ /// <param name="source">The source array.</param>
774+ /// <param name="excluding">The value to exclude from selection.</param>
775+ /// <param name="others">The additional set of optional values to exclude from selection.</param>
776+ /// <returns>The value selected.</returns>
777+ public static T RandomSelectOneExcept < T > (
778+ this T [ ] source ,
779+ T excluding , params T [ ] others )
780+ {
781+ if ( source . Length == 0 )
782+ throw new InvalidOperationException ( "Source array is empty." ) ;
783+
784+ if ( source . TryRandomSelectOneExcept ( out T value , excluding , others ) )
785+ return value ;
786+
787+ throw new InvalidOperationException ( "Exclusion set invalidates the source. No possible value can be selected." ) ;
788+ }
789+
670790 /// <summary>
671791 /// Select a random number except the excluded ones.
672792 /// </summary>
0 commit comments