|
| 1 | +/*! |
| 2 | + * @author electricessence / https://github.com/electricessence/ |
| 3 | + * Licensing: MIT https://github.com/electricessence/Genetic-Algorithm-Platform/blob/master/LICENSE.md |
| 4 | + */ |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | + |
| 10 | +namespace Open.Numeric |
| 11 | +{ |
| 12 | + public static class RandomUtilities |
| 13 | + { |
| 14 | + static Lazy<Random> R = new Lazy<Random>(() => new Random()); |
| 15 | + public static Random Random |
| 16 | + { |
| 17 | + get |
| 18 | + { |
| 19 | + return R.Value; |
| 20 | + } |
| 21 | + } |
| 22 | + public static T RandomSelectOne<T>(this IList<T> source) |
| 23 | + { |
| 24 | + return source[R.Value.Next(source.Count)]; |
| 25 | + } |
| 26 | + public static T RandomPluck<T>(this IList<T> source) |
| 27 | + { |
| 28 | + var e = source[R.Value.Next(source.Count)]; |
| 29 | + source.Remove(e); |
| 30 | + return e; |
| 31 | + } |
| 32 | + |
| 33 | + public static T RandomSelectOne<T>(this T[] source) |
| 34 | + { |
| 35 | + return source[R.Value.Next(source.Length)]; |
| 36 | + } |
| 37 | + public static T RandomSelectOne<T>(this ICollection<T> source) |
| 38 | + { |
| 39 | + return source.Skip(R.Value.Next(source.Count)).First(); |
| 40 | + } |
| 41 | + public static T RandomSelectOne<T>(this IEnumerable<T> source) |
| 42 | + { |
| 43 | + return source.Skip(R.Value.Next(source.Count())).First(); |
| 44 | + } |
| 45 | + |
| 46 | + public static int NextRandomIntegerExcluding( |
| 47 | + int range, |
| 48 | + HashSet<int> excludeSet) |
| 49 | + { |
| 50 | + if (range < 0) |
| 51 | + throw new ArgumentOutOfRangeException("range", range, "Must be a number greater than zero."); |
| 52 | + var r = new List<int>(); |
| 53 | + |
| 54 | + for (var i = 0; i < range; ++i) |
| 55 | + { |
| 56 | + if (!excludeSet.Contains(i)) r.Add(i); |
| 57 | + } |
| 58 | + |
| 59 | + return r.RandomSelectOne(); |
| 60 | + } |
| 61 | + |
| 62 | + public static uint NextRandomIntegerExcluding( |
| 63 | + uint range, |
| 64 | + HashSet<uint> excludeSet) |
| 65 | + { |
| 66 | + var r = new List<uint>(); |
| 67 | + |
| 68 | + for (uint i = 0; i < range; ++i) |
| 69 | + { |
| 70 | + if (!excludeSet.Contains(i)) r.Add(i); |
| 71 | + } |
| 72 | + |
| 73 | + return r.RandomSelectOne(); |
| 74 | + } |
| 75 | + |
| 76 | + public static uint NextRandomIntegerExcluding( |
| 77 | + int range, |
| 78 | + HashSet<uint> excludeSet) |
| 79 | + { |
| 80 | + if (range < 0) |
| 81 | + throw new ArgumentOutOfRangeException("range", range, "Must be a number greater than zero."); |
| 82 | + var r = new List<uint>(); |
| 83 | + |
| 84 | + for (uint i = 0; i < range; ++i) |
| 85 | + { |
| 86 | + if (!excludeSet.Contains(i)) r.Add(i); |
| 87 | + } |
| 88 | + |
| 89 | + return r.RandomSelectOne(); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + public static int NextRandomIntegerExcluding( |
| 94 | + int range, |
| 95 | + IEnumerable<int> excluding) |
| 96 | + { |
| 97 | + return NextRandomIntegerExcluding(range, new HashSet<int>(excluding)); |
| 98 | + } |
| 99 | + |
| 100 | + public static int NextRandomIntegerExcluding( |
| 101 | + int range, |
| 102 | + int excluding) |
| 103 | + { |
| 104 | + if (range < 0) |
| 105 | + throw new ArgumentOutOfRangeException("range", range, "Must be a number greater than zero."); |
| 106 | + var r = new List<int>(); |
| 107 | + |
| 108 | + for (var i = 0; i < range; ++i) |
| 109 | + { |
| 110 | + if (excluding != i) r.Add(i); |
| 111 | + } |
| 112 | + |
| 113 | + return r.RandomSelectOne(); |
| 114 | + } |
| 115 | + |
| 116 | + public static uint NextRandomIntegerExcluding( |
| 117 | + int range, |
| 118 | + uint excluding) |
| 119 | + { |
| 120 | + if (range < 0) |
| 121 | + throw new ArgumentOutOfRangeException("range", range, "Must be a number greater than zero."); |
| 122 | + var r = new List<uint>(); |
| 123 | + |
| 124 | + for (uint i = 0; i < range; ++i) |
| 125 | + { |
| 126 | + if (excluding != i) r.Add(i); |
| 127 | + } |
| 128 | + |
| 129 | + return r.RandomSelectOne(); |
| 130 | + } |
| 131 | + |
| 132 | + public static uint NextRandomIntegerExcluding( |
| 133 | + uint range, |
| 134 | + uint excluding) |
| 135 | + { |
| 136 | + var r = new List<uint>(); |
| 137 | + |
| 138 | + for (uint i = 0; i < range; ++i) |
| 139 | + { |
| 140 | + if (excluding != i) r.Add(i); |
| 141 | + } |
| 142 | + |
| 143 | + return r.RandomSelectOne(); |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +} |
0 commit comments