// -------------- RNG methods ------ private Numeric <T, C> ___nextFromMinusOneToPlusOne() { // Берем число в [0; 1) // и вычитаем число из [0; 1) // Получаем число от -1 до +1 :) return(Numeric <T, C> .Calculator.dif(generator.Next_SingleInterval(), generator.Next_SingleInterval())); }
/// <summary> /// Performs a high-quality random shuffling of the list. /// Uses a user-provided random number generator, which /// </summary> /// <typeparam name="T">The type of elements in the list.</typeparam> /// <typeparam name="N">The type of numbers generated by the random numbers generator.</typeparam> /// <param name="list">The calling list object.</param> /// <param name="generator">A floating-point random number generator for the <typeparamref name="N"/> type.</param> /// <param name="numericComparer">An optional numeric comparer for <typeparamref name="N"/> type. If <c>null</c>, a standard comparer will be used (if exists, otherwise, an exception will be thrown).</param> public static void Shuffle <T, N>(this IList <T> list, IRandomFloatingPoint <N> generator, IComparer <N> numericComparer = null) { Contract.Requires <ArgumentNullException>(list != null, "list"); Contract.Requires <ArgumentNullException>(generator != null, "generator"); if (numericComparer == null) { numericComparer = Comparer <N> .Default; } // Генерируем массив пар со случайными ключами, // сортируем их по ключу (а значения оказываются случайно перемешанными). List <KeyValuePair <N, T> > pairList = new List <KeyValuePair <N, T> >(list.Count); foreach (T element in list) { pairList.Add(new KeyValuePair <N, T>(generator.Next_SingleInterval(), element)); } IComparer <KeyValuePair <N, T> > pairComparer = numericComparer.createKVPComparerOnKey <N, T>(); pairList.Sort(pairComparer); // Восстанавливаем. for (int i = 0; i < pairList.Count; i++) { list[i] = pairList[i].Value; } }
/// <summary> /// Constructs a Fibonacci lagged pseudo-random number generator using the lag values and /// another generator for the first sequence members. /// /// Examples of recommended lag values: /// /// a) 17; 5 /// b) 55; 24 /// c) 97; 33. /// /// It is not recommended to provide random lag values as it will affect /// the quality of the generator randomization. /// </summary> /// <param name="firstGenerator">The IRandom(T) implementer object to receive the first values of the pseudo-random sequence.</param> /// <param name="a">The first lag of the fibonacci generator. Optional. By default, equals 97.</param> /// <param name="b">The second lag of the fibonacci generator. Optional. By default, equals 33.</param> public RandomLaggedFibonacci(IRandomFloatingPoint <double> firstGenerator, int a = 97, int b = 33) { this.a = a; this.b = b; this.max = Math.Max(a, b); for (int i = 0; i < max; i++) { list.AddLast(firstGenerator.Next_SingleInterval()); } xkmaNode = list.First; xkmbNode = list.First; for (int i = 0; i < max - a; i++) { xkmaNode = xkmaNode.Next; } for (int i = 0; i < max - b; i++) { xkmbNode = xkmbNode.Next; } return; }