Changeset 7407 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common/3.3
- Timestamp:
- 01/24/12 17:23:21 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common/3.3/ExtensionMethods.cs
r7363 r7407 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 26 27 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common { 27 28 public static class ExtensionMethods { 29 30 /// <summary> 31 /// Simply enumerates the sequence by moving over all elements. 32 /// </summary> 33 /// <typeparam name="T">The type of the items that are to be enumerated.</typeparam> 34 /// <param name="source">The enumerable that contains the items.</param> 35 public static void Enumerate<T>(this IEnumerable<T> source) { 36 var enumerator = source.GetEnumerator(); 37 while (enumerator.MoveNext()) ; 38 } 39 40 /// <summary> 41 /// Selects an element from an enumerable randomly with equal probability for each element. If the sequence is empty, selected will be false and default(T) will be returned. 42 /// </summary> 43 /// <typeparam name="T">The type of the items that are to be selected.</typeparam> 44 /// <param name="source">The enumerable that contains the items.</param> 45 /// <param name="random">The random number generator, its NextDouble() method must return a value in the range [0;1).</param> 46 /// <param name="selected">True if an element was selected, false otherwise (only because of an empty sequence).</param> 47 /// <returns>The selected item.</returns> 48 public static T ChooseRandomOrDefault<T>(this IEnumerable<T> source, IRandom random, out bool selected) { 49 if (source == null) throw new ArgumentNullException("SelectRandomOrDefault: source is null"); 50 uint counter = 1; 51 selected = false; 52 T selectedItem = default(T); 53 foreach (T item in source) { 54 if (counter * random.NextDouble() < 1.0) { // use multiplication instead of division 55 selectedItem = item; 56 selected = true; 57 } 58 counter++; 59 } 60 return selectedItem; 61 } 62 28 63 /// <summary> 29 64 /// Selects an element from an enumerable randomly with equal probability for each element. … … 32 67 /// <param name="source">The enumerable that contains the items.</param> 33 68 /// <param name="random">The random number generator, its NextDouble() method must return a value in the range [0;1).</param> 34 /// <param name="selected">True if an element was selected, false otherwise (mainly because of an empty sequence).</param>35 69 /// <returns>The selected item.</returns> 36 public static T SelectRandomOrDefault<T>(this IEnumerable<T> source, IRandom random, out bool selected) { 37 int counter = 1; 38 selected = false; 70 public static T ChooseRandom<T>(this IEnumerable<T> source, IRandom random) { 71 if (source == null) throw new ArgumentNullException("SelectRandom: source is null"); 72 if (!source.Any()) throw new ArgumentException("SelectRandom: sequence is empty."); 73 uint counter = 1; 39 74 T selectedItem = default(T); 40 75 foreach (T item in source) { 41 if ( random.NextDouble() < 1.0 / counter) {76 if (counter * random.NextDouble() < 1.0) // use multiplication instead of division 42 77 selectedItem = item; 43 selected = true; 44 } 78 counter++; 45 79 } 46 80 return selectedItem;
Note: See TracChangeset
for help on using the changeset viewer.