/// <summary>
 /// Generates an array V of size 61 (Step 2)
 /// </summary>
 /// <param name="maxValue">The maximum non-inclusive random number</param>
 private void generateVArray(int maxValue)
 {
     currentMaxValue = maxValue;
     // Step 1: Create an array V of numbers of size 61
     V = new long[vSize];
     for (int i = 0; i < vSize; i++)
     {
         V [i] = random.Next(maxValue);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 返回纯数字 字符串
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public string Number(int length)
        {
            var sb = new StringBuilder();

            for (int i = 0; i < length; i++)
            {
                sb.Append(_randomProvider.Next(10).ToString());
            }

            return(sb.ToString());
        }
Ejemplo n.º 3
0
        public void Mutate(int percentageChance)
        {
            BitArray bytes = GetCoefficients();

            for (int i = 0; i < bytes.Length; ++i)
            {
                if (_randomProvider.Next(100) < percentageChance)
                {
                    bytes[i] = !bytes.Get(i);
                }
            }

            ConvertBitArrayToCoefficients(bytes);
        }
Ejemplo n.º 4
0
        private async void GetNextWinner_OnClick(object sender, RoutedEventArgs e)
        {
            WinnerTextBlock.Visibility = Visibility.Hidden;
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += bw_DoWork;
            bw.RunWorkerAsync();
            bw.RunWorkerCompleted += (o, args) =>
            {
                var final = randomProvider.Next(_data.Count);
                SelectedComment.DataContext = _data[final];
                WinnerTextBlock.Visibility  = Visibility.Visible;
            };
        }
Ejemplo n.º 5
0
        public void TestMethod1()
        {
            IRandomProvider randomProvider = NSubstitute.Substitute.For <IRandomProvider>();

            randomProvider.Next(0, 0).ReturnsForAnyArgs(5);

            SimpleAi simpleAi = new SimpleAi(this.GameController, this.Analyzer, randomProvider, this.MillRuleEvaluator);
            Move     move     = simpleAi.Analyse();

            simpleAi.Act(move);
        }
Ejemplo n.º 6
0
        public void Mutate(int percentageChance)
        {
            BitArray bytes = GetAllCoefficientsInBytes();

            for (int i = 0; i < bytes.Length; i++)
            {
                if (_randomProvider.Next(100) < percentageChance)
                {
                    if (bytes.Get(i))
                    {
                        bytes[i] = false;
                    }
                    else
                    {
                        bytes[i] = true;
                    }
                }
            }

            ConvertByteArrayToCoefficients(bytes);
        }
Ejemplo n.º 7
0
        private void CrossoverTwoPolynomials(Polynomial poly1, Polynomial poly2)
        {
            int  cutPosition   = _randomProvider.Next((_degreeOfPolynomial + 1) * sizeof(float) * 8);
            bool fromBeginning = _randomProvider.Next(2) == 1;

            BitArray bytesOne = poly1.GetCoefficients();
            BitArray bytesTwo = poly2.GetCoefficients();

            if (fromBeginning)
            {
                for (int i = 0; i < cutPosition; ++i)
                {
                    bool bitOne = bytesOne[i];
                    bool bitTwo = bytesTwo[i];

                    if (bitOne != bitTwo)
                    {
                        bytesOne[i] = bitTwo;
                        bytesTwo[i] = bitOne;
                    }
                }
            }
            else
            {
                for (int i = cutPosition - 1; i >= 0; --i)
                {
                    bool bitOne = bytesOne[i];
                    bool bitTwo = bytesTwo[i];

                    if (bitOne != bitTwo)
                    {
                        bytesOne[i] = bitTwo;
                        bytesTwo[i] = bitOne;
                    }
                }
            }

            _crossoveredChildren.Add(new Polynomial(_randomProvider, _degreeOfPolynomial, bytesOne));
            _crossoveredChildren.Add(new Polynomial(_randomProvider, _degreeOfPolynomial, bytesTwo));
        }
Ejemplo n.º 8
0
        private Track GetNext(IList <Track> tracks)
        {
            if (tracks.Count == 0)
            {
                return(null);
            }

            var index  = _RandomProvider.Next(tracks.Count);
            var result = tracks[index];

            _PlayedTracks.Add(result);
            return(result);
        }
        public IList <T> Shuffle <T>(IList <T> list)
        {
            var listCount = list.Count;

            for (int i = 0; i < list.Count; i++)
            {
                var randomIndex = i + random.Next(listCount - i);
                var temp        = list[randomIndex];
                list[randomIndex] = list[i];
                list[i]           = temp;
            }
            return(list);
        }
Ejemplo n.º 10
0
        public Polynomial(IRandomProvider randomProvider, int degreeOfPolynomial)
        {
            if (degreeOfPolynomial < 0)
            {
                throw new WrongValueException("Degree of polynomial is less than 0");
            }

            _coefficients = new List <float>(degreeOfPolynomial + 1);

            _degree         = degreeOfPolynomial;
            _randomProvider = randomProvider;

            for (int i = 0; i < degreeOfPolynomial + 1; ++i)
            {
                _coefficients.Add(randomProvider.Next());
            }
        }
Ejemplo n.º 11
0
        // key - cumulative probability
        internal Item GetRandomItem(IDictionary <int, Item> items)
        {
            var randomNumber = randomProvider.Next(1, 101); //the maxValue is exclusive

            Item selectedItem = null;

            foreach (var item in items)
            {
                //randomNumber is betwwen 1 and 100
                if (randomNumber <= item.Key)
                {
                    selectedItem = item.Value;
                    break;
                }
            }
            return(selectedItem);
        }
        /// <summary>
        /// Adapted from https://gist.github.com/mikedugan/8249637
        /// </summary>
        /// <param name="a">The array to be shuffled</param>
        /// <returns>A shuffled array using Fisher-Yates</returns>
        public int[] Shuffle(int[] a)
        {
            int[] shuffled = a;

            // go through array, starting at the last-index
            for (var i = shuffled.Length - 1; i >= 0; i--)
            {
                var swapIndex = random.Next(i);         // get num between 0 and index, index not included
                if (swapIndex == i)
                {
                    continue;                              // don't replace with itself
                }
                var temp = shuffled[i];                    // get item at index i...
                shuffled[i]         = shuffled[swapIndex]; // set index i to new item
                shuffled[swapIndex] = temp;                // place temp-item to swap-slot
            }

            return(shuffled);
        }
Ejemplo n.º 13
0
        public IEnumerable <CatResult> GetRandomCatsFromToday()
        {
            var today        = dateTimeProvider.Now();
            var startOfToday = new DateTime(today.Year, today.Month, today.Day, 0, 0, 0);

            var random    = new Random();
            var totalCats = randomProvider.Next(10, 50);

            var cats = database
                       .GetCats()
                       .Where(c => c.CreatedOn > startOfToday && c.CreatedOn < today)
                       .Take(totalCats)
                       .Select(c => new CatResult
            {
                Name = c.Name
            })
                       .ToList();

            return(cats);
        }
Ejemplo n.º 14
0
        private void CrossoverSingle(Polynomial poly1, Polynomial poly2)
        {
            int cutPosition = _randomProvider.Next((_degreeOfPolynomial + 1) * sizeof(double) * 8);

            BitArray bytesOne = poly1.GetAllCoefficientsInBytes();
            BitArray bytesTwo = poly2.GetAllCoefficientsInBytes();

            for (int i = 0; i < cutPosition; i++)
            {
                bool bitOne = bytesOne[i];
                bool bitTwo = bytesTwo[i];

                if (bitOne != bitTwo)
                {
                    bytesOne[i] = bitTwo;
                    bytesTwo[i] = bitOne;
                }
            }

            _crossoveredChildren.Add(new Polynomial(_randomProvider, _degreeOfPolynomial, bytesOne));
            _crossoveredChildren.Add(new Polynomial(_randomProvider, _degreeOfPolynomial, bytesTwo));
        }
Ejemplo n.º 15
0
        public static T Random <T>(this IEnumerable <T> enumerable, IRandomProvider randomProvider)
        {
            if (enumerable is null)
            {
                throw new ArgumentException("Enumerable is null.", "enumerable");
            }

            if (!enumerable.Any())
            {
                throw new ArgumentException("Enumerable is empty.", "enumerable");
            }

            if (randomProvider is null)
            {
                throw new ArgumentException("Random Provider is null", "randomProvider");
            }

            var list = enumerable.ToList();
            var id   = randomProvider.Next(0, list.Count);

            return(list[id]);
        }
Ejemplo n.º 16
0
        protected override Result Apply(ref GoFishGame game)
        {
            ResultBuilder resultBuilder = new ResultBuilder();

            game.Deck.Shuffle(_randomProvider);

            for (int i = 0; i < game.RuleSet.CardsOnStart; i++)
            {
                foreach (Player player in game.Players)
                {
                    if (game.Deck.TryDraw(out Card card))
                    {
                        player.Cards.Add(card);
                    }
                }
            }

            game.GameState = GameState.Playing;
            resultBuilder.AddFeedback($"The game was started!");

            switch (game.RuleSet.StartBehaviour)
            {
            case StartBehaviour.FirstPlayer:
                game.CurrentPlayerIndex = 0;
                break;

            case StartBehaviour.RandomPlayer:
                game.CurrentPlayerIndex = _randomProvider.Next(game.Players.Count);
                break;

            default:
                throw new ArgumentException("Unknown option for StartBehaviour", nameof(Models.GoFishGame.RuleSet));
            }
            resultBuilder.AddFeedback($"The turn was passed to {game.CurrentPlayer.Username}");

            return(resultBuilder.Build());
        }
Ejemplo n.º 17
0
        public int Pick(IRandomProvider randomProvider, EnvelopePair envelopePair)
        {
            int amount = 0;
            int randomValue = randomProvider.Next(1, max);

            /*
             * То есть чем меньше сумма в конверте А, тем с большей вероятностью следует сменить конверт и наоборот,
             * несколько большая сумма в А говорит о том, что скорее следует оставить первый конверт себе.
             */

            if (envelopePair.FirstEnvelope.Amount <= randomValue)
            {
                amount = envelopePair.SecondEnvelope.Amount;
            }
            else
            {
                amount = envelopePair.FirstEnvelope.Amount;
            }

            if (amount > max)
                max = amount;

            return amount;
        }
Ejemplo n.º 18
0
 public void Shuffle(IRandomProvider randomProvider = null)
 {
     randomProvider = randomProvider ?? RandomProvider.GetInstance();
     _stack.Sort((c1, c2) => randomProvider.Next(-100, 100));
 }