コード例 #1
0
            public int Next()
            {
                var realNext = _next == _min ? _min : _base.Next(_min, _next);

                _next = _base.Next(_min, _max);
                return(realNext);
            }
        RandomCombination(
            int n,
            int k
            )
        {
            bool[] selection = new bool[n];
            if (k * 3 < n)
            {
                // just pick and try
                int selectionCount = 0;
                while (selectionCount < k)
                {
                    int index = _random.Next(n);
                    if (!selection[index])
                    {
                        selection[index] = true;
                        selectionCount++;
                    }
                }

                return(selection);
            }
            else
            {
                // based on permutation
                int[] permutation = RandomPermutation(n);
                for (int i = 0; i < k; i++)
                {
                    selection[permutation[i]] = true;
                }

                return(selection);
            }
        }
コード例 #3
0
 public DoubleRundom(RandomSource @base, int min, int max)
 {
     _base = @base;
     _min  = min;
     _max  = max;
     _next = @base.Next(min, max);
 }
コード例 #4
0
        /// <inheritdoc/>
        protected override int GetNextRandom(int sides)
        {
            if (sides < 2)
            {
                throw new ArgumentOutOfRangeException(nameof(sides));
            }

            return(randomSource.Next(0, sides) + 1);
        }
コード例 #5
0
ファイル: RangeIntExt.cs プロジェクト: Noggog/CSharpExt
 public static int Get(this RangeInt32 range, RandomSource rand)
 {
     if (range.Min == range.Max)
     {
         return(range.Min);
     }
     else
     {
         return(rand.Next(range.Min, range.Max + 1));
     }
 }
コード例 #6
0
        private static void Shuffle <T>(this IList <T> list)
        {
            int n = list.Count;

            while (n > 1)
            {
                n--;
                int k     = RandomSource.Next(n + 1);
                T   value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: lanicon/Neatron
            private ParetoFrontPoint MakeTournamentSelection(IReadOnlyList <ParetoFrontPoint> paretoFrontItems)
            {
                var best = paretoFrontItems[RandomSource.Next(paretoFrontItems.Count)];

                for (var i = 2; i <= _populationParameters.TournamentSize; i++)
                {
                    var next = paretoFrontItems[RandomSource.Next(paretoFrontItems.Count)];
                    if (next.IsBetterThan(best))
                    {
                        best = next;
                    }
                }

                return(best);
            }
コード例 #8
0
ファイル: Program.cs プロジェクト: lanicon/Neatron
            private Genome Reproduce(IReadOnlyList <ParetoFrontPoint> paretoFrontPoints)
            {
                var p1 = MakeTournamentSelection(paretoFrontPoints);

                if (RandomSource.Next() < 0.8f)
                {
                    return(_neatPopulation.Replicator.Reproduce(p1.Genome));
                }

                var p2 = MakeTournamentSelection(paretoFrontPoints);

                return(p1.Fitness > p2.Fitness
                    ? _neatPopulation.Replicator.Reproduce(p1.Genome, p2.Genome)
                    : _neatPopulation.Replicator.Reproduce(p2.Genome, p1.Genome));
            }
コード例 #9
0
        public void NextTest(int min, int max)
        {
            var uut = new RandomSource();

            var random = uut.Next(min, max);

            if (max > min)
            {
                Assert.True(random >= min);
                Assert.True(random <= max);
            }
            else
            {
                Assert.True(random >= max);
                Assert.True(random <= min);
            }
        }
コード例 #10
0
        /// <returns>
        /// The simulated time.
        /// TimeSpan.Zero if no time progress was achieved due to delta events.
        /// TimeSpan.MinValue if no events were available.
        /// </returns>
        private TimeSpan RunSignalAssignmentPhase(bool progressTime)
        {
            _phase = SchedulerPhase.SignalAssignment;
            TimeSpan simulatedTime = TimeSpan.Zero;

            while (progressTime && _deltaEvents.Count == 0)
            {
                TimeSpan timespan;
                if (_timeline.TryNextEventTime(out timespan))
                {
                    _schedule.ProgressTime(timespan, this);
                    OnSimulationTimeProgress(timespan);
                    simulatedTime += timespan;
                }
                else
                {
                    _phase = SchedulerPhase.Idle;
                    return(TimeSpan.MinValue);
                    // TODO: check, why MinValue?
                }
            }

            if (_deltaEvents.Count > 0)
            {
                int index = _random.Next(_deltaEvents.Count);
                SchedulerEventItem item = _deltaEvents[index];
                _deltaEvents.RemoveAt(index);

                ISchedulable subject = item.Subject;
                if (subject.CurrentValue == null || !subject.CurrentValue.Equals(item.Value))
                {
                    item.Subject.CurrentValue = item.Value;
                    _schedulablesWithEvent.Add(item.Subject);
                }
            }

            _phase = SchedulerPhase.Idle;
            return(simulatedTime);
        }
コード例 #11
0
 public Int32 RollDie(int sides)
 {
     return(_source.Next(0, sides) + 1);
 }
コード例 #12
0
        public IFieldsGraph Build()
        {
            var blackCount = Math.Floor(blackProbability * _rowCount * _columnCount);

            _rowCount    = _rowCount + 4;
            _columnCount = _columnCount + 2;


            var input = new FieldType[_rowCount][];

            for (int row = 0; row < _rowCount; ++row)
            {
                input[row] = new FieldType[_columnCount];
                for (int column = 0; column < _columnCount; ++column)
                {
                    input[row][column] = FieldType.Empty;
//                    if (row < 2 || column < 1 || row >= _rowCount - 2 || column >= _columnCount - 1)
//                    {
//                        continue;
//                    }
//                    var placeBlack = _random.NextDouble() < blackProbability;
//                    if (placeBlack)
//                    {
//                        input[row][column] = FieldType.Black;
//                    }
                }
            }

            while (blackCount > 0)
            {
                var rowIndex = RandomSource.Next(2, _rowCount - 2);
                var colIndex = RandomSource.Next(1, _columnCount - 1);
                if (input[rowIndex][colIndex] == FieldType.Empty)
                {
                    input[rowIndex][colIndex] = FieldType.Black;
                    blackCount--;
                }
            }

            bool whiteWasPlaced = false;

            for (int row = _rowCount / 2, row2 = _rowCount / 2 + 1;
                 row >= 2 && row2 < _rowCount - 2 && !whiteWasPlaced;
                 --row, ++row2)
            {
                for (int col = (_columnCount) / 2, col2 = (_columnCount / 2) + 1;
                     col >= 1 && col2 <= _columnCount - 1;
                     ++col2,
                     --col)
                {
                    if (input[row][col] == FieldType.Empty)
                    {
                        input[row][col] = FieldType.White;
                        whiteWasPlaced  = true;
                        break;
                    }
                    if (input[row2][col] == FieldType.Empty)
                    {
                        input[row2][col] = FieldType.White;
                        whiteWasPlaced   = true;
                        break;
                    }
                    if (input[row][col2] == FieldType.Empty)
                    {
                        input[row][col2] = FieldType.White;
                        whiteWasPlaced   = true;
                        break;
                    }
                    if (input[row2][col2] == FieldType.Empty)
                    {
                        input[row2][col2] = FieldType.White;
                        whiteWasPlaced    = true;
                        break;
                    }
                }
            }
            if (whiteWasPlaced == false)
            {
                throw new InvalidOperationException();
            }

            return(new TestFieldsGraph(input).Build());
        }
コード例 #13
0
        /// <summary>
        /// Gets a list of random indices into an array, depending on various settings.
        /// </summary>
        /// <param name="numberOfLocationsToChooseFrom">The array length, indexes to which will be returned.</param>
        /// <param name="maximumNumberOfLocations">The maximum number of locations to select.</param>
        /// <param name="selectionWithReplacement"><see langword="true"/> if the same location can be returned more than once.</param>
        /// <param name="lambda">The probability of choosing a location at all.</param>
        /// <returns>A list of locations in the original array.</returns>
        public int[] GetLocations(
            int numberOfLocationsToChooseFrom,
            int maximumNumberOfLocations  = 1,
            bool selectionWithReplacement = false,
            double lambda = 0.1)
        {
            if (maximumNumberOfLocations <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maximumNumberOfLocations),
                                                      "Maximum number of locations must be greater than 0.");
            }

            if (!selectionWithReplacement && maximumNumberOfLocations > numberOfLocationsToChooseFrom)
            {
                throw new ArgumentOutOfRangeException(nameof(maximumNumberOfLocations),
                                                      "If sampling without replacement, cannot ask for more locations than are available.");
            }

            if (lambda < 0 || lambda > 1)
            {
                throw  new ArgumentOutOfRangeException(nameof(lambda),
                                                       "The probability of selecting a location must be between 0 and 1.");
            }

            if (lambda == 0)
            {
                return(new int[0]);
            }

            if (lambda == 1 && !selectionWithReplacement)
            {
                // There's a fast function implemented for this...
                return(Enumerable.Range(0, numberOfLocationsToChooseFrom)
                       .SelectCombination(maximumNumberOfLocations, Rng)
                       .ToArray());
            }

            var locations = new List <int>();
            var i         = 0;

            while (i < maximumNumberOfLocations)
            {
                // See if we will make a selection
                var mutate = Rng.NextDouble() < lambda;

                if (mutate)
                {
                    // See if we need to reduce the randomisation space
                    var offset = selectionWithReplacement
                        ? 0
                        : locations.Count;

                    // Generate a value
                    var location = Rng.Next(0, numberOfLocationsToChooseFrom - offset);

                    if (selectionWithReplacement)
                    {
                        // We are generating with replacement - just add to list.
                        locations.Add(location);
                    }
                    else
                    {
                        // Find the true value which the truncated space refers to
                        while (locations.Contains(location))
                        {
                            location++;
                        }
                        // Add to list
                        locations.Add(location);
                    }
                }

                i++;
            }

            return(locations.ToArray());
        }
 NextInt32()
 {
     return(RandomSource.Next(_a, _b + 1));
 }
コード例 #15
0
ファイル: VariableDiscrete.cs プロジェクト: lulzzz/PopOptBox
 /// <summary>
 /// Implements random number generation for this variable.
 /// </summary>
 /// <returns>A legal object (int).</returns>
 public object GetNextRandom(RandomSource rng)
 {
     return(rng.Next(lowerBoundForGeneration, upperBoundForGeneration));
 }
コード例 #16
0
ファイル: EnumerableExt.cs プロジェクト: Noggog/CSharpExt
 public static IEnumerable <T> Randomize <T>(this IEnumerable <T> e, RandomSource rand)
 {
     return(e.OrderBy <T, int>((item) => rand.Next()));
 }