1- /*!
1+ /*!
22 * @author electricessence / https://github.com/electricessence/
33 * Licensing: MIT https://github.com/electricessence/Genetic-Algorithm-Platform/blob/master/LICENSE.md
44 */
@@ -11,7 +11,7 @@ namespace Open.Numeric
1111{
1212 public static class RandomUtilities
1313 {
14- static Lazy < Random > R = new Lazy < Random > ( ( ) => new Random ( ) ) ;
14+ static readonly Lazy < Random > R = new Lazy < Random > ( ( ) => new Random ( ) ) ;
1515 public static Random Random
1616 {
1717 get
@@ -20,12 +20,11 @@ public static Random Random
2020 }
2121 }
2222
23-
2423 public static bool TryRandomPluck < T > ( this LinkedList < T > source , out T value )
2524 {
2625 if ( source . Count == 0 )
2726 {
28- value = default ( T ) ;
27+ value = default ;
2928 return false ;
3029 }
3130
@@ -40,7 +39,7 @@ public static bool TryRandomPluck<T>(this LinkedList<T> source, out T value)
4039
4140 public static T RandomPluck < T > ( this LinkedList < T > source )
4241 {
43- if ( source . TryRandomPluck ( out T value ) )
42+ if ( source . TryRandomPluck ( out var value ) )
4443 return value ;
4544
4645 throw new InvalidOperationException ( "Source collection is empty." ) ;
@@ -50,7 +49,7 @@ public static bool TryRandomPluck<T>(this IList<T> source, out T value)
5049 {
5150 if ( source . Count == 0 )
5251 {
53- value = default ( T ) ;
52+ value = default ;
5453 return false ;
5554 }
5655
@@ -62,7 +61,7 @@ public static bool TryRandomPluck<T>(this IList<T> source, out T value)
6261
6362 public static T RandomPluck < T > ( this IList < T > source )
6463 {
65- if ( source . TryRandomPluck ( out T value ) )
64+ if ( source . TryRandomPluck ( out var value ) )
6665 return value ;
6766
6867 throw new InvalidOperationException ( "Source collection is empty." ) ;
@@ -76,20 +75,19 @@ public static bool TryRandomSelectOne<T>(
7675 var count = source . Count ;
7776 if ( count == 0 )
7877 {
79- value = default ( T ) ;
78+ value = default ;
8079 return false ;
8180 }
8281
83- if ( excluding == null || excluding . Count == 0 )
84- {
85- value = source [ R . Value . Next ( count ) ] ;
86- return true ;
87- }
82+ if ( excluding != null && excluding . Count != 0 )
83+ return source
84+ . Where ( o => ! excluding . Contains ( o ) )
85+ . ToArray ( )
86+ . TryRandomSelectOne ( out value ) ;
87+
88+ value = source [ R . Value . Next ( count ) ] ;
89+ return true ;
8890
89- return source
90- . Where ( o => ! excluding . Contains ( o ) )
91- . ToArray ( )
92- . TryRandomSelectOne ( out value ) ;
9391 }
9492
9593 public static T RandomSelectOne < T > (
@@ -106,14 +104,14 @@ public static T RandomSelectOne<T>(
106104 }
107105
108106 public static bool TryRandomSelectOneExcept < T > (
109- this IReadOnlyList < T > source ,
107+ this IReadOnlyCollection < T > source ,
110108 T excluding ,
111109 out T value )
112110 {
113111 var count = source . Count ;
114112 if ( count == 0 )
115113 {
116- value = default ( T ) ;
114+ value = default ;
117115 return false ;
118116 }
119117
@@ -124,7 +122,7 @@ public static bool TryRandomSelectOneExcept<T>(
124122 }
125123
126124 public static T RandomSelectOneExcept < T > (
127- this IReadOnlyList < T > source ,
125+ this IReadOnlyCollection < T > source ,
128126 T excluding )
129127 {
130128 if ( source . Count == 0 )
0 commit comments