Exemplo n.º 1
0
        public static IEnumerable <Tuple <T, T> > ToRandomPairs <T>(this IEnumerable <T> source, IRando randy)
        {
            var srcList = source.ToList();

            while (srcList.Count > 1)
            {
                var dex = randy.NextInt(srcList.Count);
                var p1  = srcList[dex];
                srcList.RemoveAt(dex); dex = randy.NextInt(srcList.Count);
                var p2 = srcList[dex];
                srcList.RemoveAt(dex);
                yield return(new Tuple <T, T>(p1, p2));
            }
        }
Exemplo n.º 2
0
 public static IEnumerable <int> ToIntEnumerator(this IRando rando)
 {
     while (true)
     {
         yield return(rando.NextInt());
     }
 }
Exemplo n.º 3
0
 public static IEnumerable <IRando> ToRandomEnumerator(this IRando rando)
 {
     while (true)
     {
         yield return(Fast(rando.NextInt()));
     }
 }
Exemplo n.º 4
0
 public static IEnumerable <T> Pick <T>(this IRando rando, IReadOnlyList <T> items)
 {
     while (true)
     {
         yield return(items[rando.NextInt(items.Count)]);
     }
 }
Exemplo n.º 5
0
        public static GenomeSorterStageDimer Mutate(this GenomeSorterStageDimer genomeDimer, IRando rando)
        {
            var        mutantIndex  = rando.NextInt(genomeDimer.GenomeStageDimers.Length);
            var        gsdToReplace = genomeDimer.GenomeStageDimers[mutantIndex];
            StageDimer gsdMutant    = gsdToReplace.Mutate(rando);

            return(genomeDimer.GenomeStageDimers
                   .ReplaceAtIndex((uint)mutantIndex, gsdMutant)
                   .ToGenomeDimer(Guid.NewGuid()));
        }
Exemplo n.º 6
0
        public static IEnumerable <Tuple <T, T> > PairRandomly <T>(this IReadOnlyList <T> items, IRando rando)
        {
            var workingList = items.ToList();

            if (workingList.Count % 2 == 1)
            {
                throw new ArgumentException("An odd number of items was sent to PairRandomly");
            }
            while (workingList.Any())
            {
                var nextIndex = rando.NextInt() % workingList.Count();
                var itemA     = workingList[nextIndex];
                workingList.RemoveAt(nextIndex);
                nextIndex = rando.NextInt() % workingList.Count();
                var itemB = workingList[nextIndex];
                workingList.RemoveAt(nextIndex);

                yield return(new Tuple <T, T>(itemA, itemB));
            }
        }
Exemplo n.º 7
0
 public static T SelectFromRemaining <T>(this IRando rando, T[] values, bool[] rem)
 {
     while (true)
     {
         var dex = rando.NextInt(values.Length);
         if (rem[dex])
         {
             rem[dex] = false;
             return(values[dex]);
         }
     }
 }
Exemplo n.º 8
0
        public static T[] FisherYatesShuffle <T>(this IReadOnlyList <T> origList, IRando rando)
        {
            var arrayLength = origList.Count;
            var retArray    = origList.ToArray();

            for (var i = arrayLength - 1; i > 0; i--)
            {
                var j    = rando.NextInt(i + 1);
                var temp = retArray[i];
                retArray[i] = retArray[j];
                retArray[j] = temp;
            }
            return(retArray);
        }
Exemplo n.º 9
0
        public static void SelectWithoutReplacement <T>(this IRando rando, T[] values, T[] trs)
        {
            if (trs.Length > values.Length)
            {
                throw new Exception("(trs.Length > values.Length) in SelectWithoutReplacement");
            }
            var l = values.ToList();

            while (values.Length - l.Count < trs.Length)
            {
                var np = rando.NextInt(l.Count - 1);
                trs[values.Length - l.Count] = values[np];
                l.RemoveAt(np);
            }
        }
Exemplo n.º 10
0
        public static T[] FisherYatesPartialShuffle <T>(this IReadOnlyList <T> origList, IRando rando, double mixingRate)
        {
            var arrayLength = origList.Count;
            var retArray    = origList.ToArray();

            for (var i = arrayLength - 1; i > 0; i--)
            {
                if (rando.NextDouble() > mixingRate)
                {
                    continue;
                }
                var j    = rando.NextInt(i + 1);
                var temp = retArray[i];
                retArray[i] = retArray[j];
                retArray[j] = temp;
            }
            return(retArray);
        }
Exemplo n.º 11
0
        public static GaData ToStageDimerGaData(
            this IRando randy, uint order,
            uint sorterCount, uint sortableCount, uint stageCount,
            double sorterWinRate, double sortableWinRate)
        {
            var randomSortablePool = randy.ToRandomSortablePool(order, sortableCount);
            var dimerGenomePool    = randy.ToGenomePoolStageDimer(order, stageCount, sorterCount);

            var d = new Dictionary <string, object>();

            d.SetCurrentStep(0);
            d.SetSeed(randy.NextInt());
            d.SetSorterWinRate(sorterWinRate);
            d.SetSortableWinRate(sortableWinRate);
            d.SetSortablePool(randomSortablePool);
            d.SetDimerGenomePool(dimerGenomePool);

            return(new GaData(d));
        }
Exemplo n.º 12
0
        public static GaData ToDirectGaSortingData(
            this IRando randy, uint order,
            uint sorterCount, uint sortableCount, uint stageCount,
            double sorterWinRate, double sortableWinRate,
            StageReplacementMode stageReplacementMode)
        {
            var randomSortablePool = randy.ToRandomSortablePool(order, sortableCount);
            var randomSorterPool   = randy.ToRandomSorterPool(order, stageCount, sorterCount);

            var d = new Dictionary <string, object>();

            d.SetCurrentStep(0);
            d.SetSeed(randy.NextInt());
            d.SetSorterWinRate(sorterWinRate);
            d.SetSortableWinRate(sortableWinRate);
            d.SetSortablePool(randomSortablePool);
            d.SetSorterPool(randomSorterPool);
            d.SetStageReplacementMode(stageReplacementMode);

            return(new GaData(d));
        }
Exemplo n.º 13
0
 public static IRando Spawn(this IRando rando)
 {
     return(new RandoFast(rando.NextInt()));
 }
Exemplo n.º 14
0
 public static IEnumerable <Guid> ToGuidEnumerator(this IRando rando)
 {
     while (true)
     {
         yield return(new Guid
                      (
                          (uint)rando.NextInt(),
                          (ushort)rando.NextInt(),
                          (ushort)rando.NextInt(),
                          (byte)rando.NextInt(),
                          (byte)rando.NextInt(),
                          (byte)rando.NextInt(),
                          (byte)rando.NextInt(),
                          (byte)rando.NextInt(),
                          (byte)rando.NextInt(),
                          (byte)rando.NextInt(),
                          (byte)rando.NextInt()
                      ));
     }
 }
Exemplo n.º 15
0
 public static Guid NextGuid(this IRando rando)
 {
     return(new Guid
            (
                (uint)rando.NextInt(),
                (ushort)rando.NextInt(),
                (ushort)rando.NextInt(),
                (byte)rando.NextInt(),
                (byte)rando.NextInt(),
                (byte)rando.NextInt(),
                (byte)rando.NextInt(),
                (byte)rando.NextInt(),
                (byte)rando.NextInt(),
                (byte)rando.NextInt(),
                (byte)rando.NextInt()
            ));
 }