public void Spin_RandomizerReturnsIllegalValue_ExceptionThrown() { fakeRandomizer.Next().Returns(40U); // Always return '40' from randomizer var uut = new RouletteGame.Roulette.Roulette(fakeFieldFactory, fakeRandomizer); Assert.That(() => uut.Spin(), Throws.TypeOf <RouletteGameException>()); }
public void Teach(IBrain brain, int iterations) { var analyser = new Analyser(brain); var depth = 4; for (var i = 0; i < iterations; i++) { var stones = _randomizer.Next(1, 48); var position = GetAwariPosition(stones); var p = position.GetPits(); if (p[0] > 0 || p[1] > 0 || p[2] > 0 || p[3] > 0 || p[4] > 0 || p[5] > 0) { analyser.Start(i, p, 48 - stones); var score = _evaluator.Evaluate(position, 5) + 48 - stones; brain.Learn(p, 48 - stones, score); analyser.Stop(score); } } analyser.Dispose(); }
private Particle GenerateNewParticle(Vector2 position, float height, float width, Vector2 velocity, Vector2 gravity, Color[] colors, TimeSpan lifeTime) { var angle = 0; var angularVelocity = 10f * (_randomizer.NextDouble() * 2 - 1); var color = colors[_randomizer.Next(colors.Length)]; return(new Particle(position, height, width, velocity, angle, angularVelocity, color, gravity, lifeTime)); }
public void Reset() { Position = new Vector2(0); Acceleration = new Vector2(_randomizer.Next(0, 20), _randomizer.Next(0, 20)); //Acceleration = new Vector2(1,1); Speed = 100; Speed = (_randomizer.NextFloat() + 1) * 50; Size = new Vector2(_randomizer.NextFloat() + 0.3f); AngularVelocity = (_randomizer.NextFloat() / 4f) - .125f; }
public Tuple <MoveList, MoveList> DoCrossover(MoveList firstMovelist, MoveList secondMovelist) { var child1 = new MoveList(); var child2 = new MoveList(); if (firstMovelist.Count >= 3) { var firstCrossoverPoint = _randomizer.Next(1, firstMovelist.Count - 1); var secondCrossoverPoint = _randomizer.Next(1, firstMovelist.Count - 1); while (secondCrossoverPoint == firstCrossoverPoint) { secondCrossoverPoint = _randomizer.Next(1, firstMovelist.Count - 1); } var lowestCrossoverPoint = (firstCrossoverPoint < secondCrossoverPoint ? firstCrossoverPoint : secondCrossoverPoint); var highestCrossoverPoint = (firstCrossoverPoint > secondCrossoverPoint ? firstCrossoverPoint : secondCrossoverPoint); var difference = highestCrossoverPoint - lowestCrossoverPoint; child1.AddRange( firstMovelist.Take(lowestCrossoverPoint) .Concat(secondMovelist.Skip(lowestCrossoverPoint).Take(difference)) .Concat(firstMovelist.Skip(highestCrossoverPoint)) ); child2.AddRange( secondMovelist.Take(lowestCrossoverPoint) .Concat(firstMovelist.Skip(lowestCrossoverPoint).Take(difference)) .Concat(secondMovelist.Skip(highestCrossoverPoint)) ); var children = new Tuple <MoveList, MoveList>(child1, child2); return(children); } else { var performOnePointCrossover = _randomizer.NextBoolean(); //Not using the crossover factory to make sure a TwoPointCrossover won't be created. if (performOnePointCrossover) { var opc = new OnePointCrossover(_randomizer); var children = opc.DoCrossover(firstMovelist, secondMovelist); return(children); } else { var oc = new OrderedCrossover(_randomizer); var children = oc.DoCrossover(firstMovelist, secondMovelist); return(children); } } }
public DroneSupervisor(Map map, IRandomizer randomizer) { _map = map; GoalValue = 0; for (var y = 0; y < map.Size; ++y) GoalValue += map[randomizer.Next(0, map.Size), y]; }
public void SpinRoulette_RouletteIsSpun_ResultFieldAnnounced() { _randomizer.Next().Returns((uint)2); _game.OpenBets(); _game.CloseBets(); _game.SpinRoulette(); // Field number 2 has number2, black, even _output.Received().Report(Arg.Is <string>(str => str.ToLower().Contains("result:") && str.ToLower().Contains("2") && str.ToLower().Contains("black") )); }
public void Reset(int numberOfAnimals = 10) { _animals.Clear(); var animalTypes = GetAnimalTypes(); for (int i = 0; i < numberOfAnimals; ++i) { var type = animalTypes[_randomizer.Next(animalTypes.Count)]; var name = _animalNamesDatabase.GetRandomName(); var age = TimeSpan.FromDays(_randomizer.Next(3650)); var animal = ConstructAnimal(type, Guid.NewGuid(), name, age); _animals.Add(animal.Id, animal); } Save(); }
public void Next_WhenMaxValueLowerThanMinValue_ThrowsArgumentException() { int minValue = _random.Next(1, 100); int maxValue = minValue - _random.Next(1, 100); IRandomizer sut = CreateSut(); sut.Next(minValue, maxValue); }
public void PayUp_EvenOddBetPlaced_WinnerIsAnnouncedCorrectly() { _randomizer.Next().Returns((uint)2); // Requires knowledge that Fields[2] is even _game.OpenBets(); _game.PlaceBet(_evenOddBet); _game.CloseBets(); _game.SpinRoulette(); _game.PayUp(); _output.Received().Report(Arg.Is <string>(str => str.ToLower().Contains("berit") && str.ToLower().Contains("200$") && str.ToLower().Contains("even") )); }
/// <summary> /// 난수발생기로 지정한 갯수만큼 난수를 발생시킵니다. /// </summary> /// <param name="randomizer"></param> /// <param name="count"></param> /// <returns></returns> public static IEnumerable <double> GenerateValues(this IRandomizer randomizer, int count) { randomizer.ShouldNotBeNull("randomizer"); count.ShouldBePositive("count"); for (var i = 0; i < count; i++) { yield return(randomizer.Next()); } }
public string GetRandomName() { Debug.Assert(_names.Count != 0); var index = _randomizer.Next(_names.Count); var name = _names[index]; _names.RemoveAt(index); return(name); }
public void Load(Texture2D texture2D) { for (int i = 0; i < 10; i++) { var sprite = new Astroid(_randomizer); sprite.Texture2D = texture2D; sprite.Source = _astroidPictures[_randomizer.Next(_astroidPictures.Count)]; Sprites.Add(sprite); } Reset(); }
public void Next_WhenCalled_ReturnsValueInInterval() { int minValue = _random.Next(1, 100); int maxValue = minValue + _random.Next(1, 100); IRandomizer sut = CreateSut(); int result = sut.Next(minValue, maxValue); Assert.IsTrue(result >= minValue && result <= maxValue); }
/// <summary> /// 난수발생기를 이용하여 <paramref name="values"/> 배열에 난수를 <paramref name="count"/> 갯수만큼 할당합니다. /// </summary> public static void FillValues(this IRandomizer randomizer, double[] values, int count) { randomizer.ShouldNotBeNull("randomizer"); values.ShouldNotBeEmpty("values"); Guard.Assert(values.Length >= count, "values 의 length가 count 보다 작습니다."); for (int i = 0; i < count; i++) { values[i] = randomizer.Next(); } }
private void Shuffle <T>(IList <T> source) { var n = source.Count; for (var i = 0; i < n; i++) { var r = i + _randomizer.Next(0, n - i); var temp = source[i]; source[i] = source[r]; source[r] = temp; } }
public void Spin() { var n = _randomizer.Next(); if (Fields.Count > n) { _result = Fields[(int)n]; } else { throw new RouletteGameException($"Illegal field number: {n}"); } }
public MoveList Mutate(MoveList moveListToMutate) { var mutantMoveList = new MoveList(); var coordinates = moveListToMutate.Select(move => move.Coordinate).ToList(); var indexOfCoordinateToMove = _randomizer.Next(moveListToMutate.Count); var indexToMoveTo = _randomizer.Next(moveListToMutate.Count); var coordinateToMove = coordinates[indexOfCoordinateToMove]; coordinates.RemoveAt(indexOfCoordinateToMove); coordinates.Insert(indexToMoveTo, coordinateToMove); for (var i = 0; i < moveListToMutate.Count; i++) { mutantMoveList.Add( new Move( new Piece(moveListToMutate[i].Piece.Type, moveListToMutate[i].Piece.OuterColor, moveListToMutate[i].Piece.InnerColor), coordinates[i]) ); } return(mutantMoveList); }
private Particle GenerateNewParticle(List <Color> colors) { Texture2D texture = _textures[_random.Next(_textures.Count)]; Vector2 position = EmitterLocation; Vector2 velocity = new Vector2( 1f * (float)(_random.NextFloat() * 2 - 1), 1f * (float)(_random.NextFloat() * 2 - 1)); float angle = 0; float angularVelocity = 0.1f * (float)(_random.NextFloat() * 2 - 1); var color = colors[_random.Next(colors.Count)]; //Color color = new Color( // (float)random.NextFloat(), // (float)random.NextFloat(), // (float)random.NextFloat()); //var color = Color.LightGray; float size = (float)_random.NextFloat(); int ttl = 20 + _random.Next(40); return(new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl)); }
private static List<T> Shuffle<T>(IRandomizer random, IEnumerable<T> list) { List<T> newList = list.Select(x => x).ToList(); for (int i = newList.Count; i > 1; i--) { // Pick random element to swap. int j = random.Next(i); // 0 <= j <= i-1 // Swap. T tmp = newList[j]; newList[j] = newList[i - 1]; newList[i - 1] = tmp; } return newList; }
public int Roll(string diceExpression) { var expressionParts = diceExpression.Split('d'); var dieCount = Convert.ToInt32(expressionParts[0]); var sideCount = Convert.ToInt32(expressionParts[1]); var sum = 0; for (var i = 0; i < dieCount; i++) { sum += _randomizer.Next(sideCount) + 1; } return(sum); }
public WeatherInfo GetWeatherInfo() { var items = new[] { new WeatherInfo { City = "Santa Monica", Condition = "Sunny", IconUrl = "~/modules/harvest.orcharddevtoolbelt/content/images/sunny.png", Temperature = "20° C" }, new WeatherInfo { City = "Santa Monica", Condition = "Rainy", IconUrl = "~/modules/harvest.orcharddevtoolbelt/content/images/rainy.png", Temperature = "15° C" }, new WeatherInfo { City = "Santa Monica", Condition = "Snowy", IconUrl = "~/modules/harvest.orcharddevtoolbelt/content/images/snowy.png", Temperature = "-10° C" } }; return(items[_randomizer.Next(0, items.Length)]); }
private Uri GetImageUrl(IRedditLink link) { if (link == null) { throw new ArgumentNullException(nameof(link)); } IEnumerable <Uri> imageUrls = GetImagesUrls(link); if (imageUrls.Any() == false) { return(null); } int elementNo = _randomizer.Next(0, imageUrls.Count() - 1); return(imageUrls.ElementAt(elementNo)); }
public static IEnumerable <T> Shuffle <T>(this IList <T> collection, IRandomizer provider, int itemsToTake) { IList <T> ts; ts = (collection == null ? new List <T>(0) : collection.ToList <T>()); IList <T> ts1 = ts; int num = Math.Min(ts1.Count, itemsToTake); if (ts1.Count > 0) { for (int i = 1; i <= num; i++) { int num1 = provider.Next(ts1.Count); yield return(ts1[num1]); ts1.RemoveAt(num1); } } }
public Tuple <MoveList, MoveList> DoCrossover(MoveList firstMovelist, MoveList secondMovelist) { var child1 = new MoveList(); var child2 = new MoveList(); if (firstMovelist.Count > 2) { //If crossover = 0, you simply swap the entire sequence. This doesn't yield a new child //Same if the crossoverPoint = length - 1. var elementsToSkip = _randomizer.Next(1, firstMovelist.Count - 2); child1.AddRange(firstMovelist.Take(elementsToSkip).Concat(secondMovelist.Skip(elementsToSkip))); child2.AddRange(secondMovelist.Take(elementsToSkip).Concat(firstMovelist.Skip(elementsToSkip))); } else if (firstMovelist.Count == 1) { child1.Add(firstMovelist[0]); child2.Add(secondMovelist[0]); } else { var swapFirstMove = _randomizer.NextBoolean(); if (swapFirstMove) { child1.Add(secondMovelist[0]); child1.Add(firstMovelist[1]); child2.Add(firstMovelist[0]); child2.Add(secondMovelist[1]); } else { child1.Add(firstMovelist[0]); child1.Add(secondMovelist[1]); child2.Add(secondMovelist[0]); child2.Add(firstMovelist[1]); } } var children = new Tuple <MoveList, MoveList>(child1, child2); return(children); }
public void FieldBet_CorrectNumber_AmountCorrect() { _fakeRandomizer.Next().Returns(2U); _rouletteGame.OpenBets(); _rouletteGame.PlaceBet(_fieldBet); _rouletteGame.CloseBets(); _rouletteGame.SpinRoulette(); _rouletteGame.PayUp(); _fakeOutput.Received(1).Report(Arg.Is <string>(s => s.ToLower().Contains("3600") && s.ToLower().Contains("bente"))); }
public static ICrossoverStrategy GetCrossoverStrategy(IRandomizer r) { var x = r.Next(101); if (x >= 0 && x <= 50) { return(new OrderedCrossover(r)); } if (x >= 51 && x <= 75) { return(new OnePointCrossover(r)); } if (x >= 76) { return(new TwoPointCrossover(r)); } return(new OrderedCrossover(r)); }
public List <Card> Shuffle(IEnumerable <Card> inputCards) { if (inputCards == null) { return(new List <Card>()); } var inputCopy = inputCards.ToList(); var shuffledCards = new List <Card>(); int pos; while (inputCopy.Count > 0) { pos = _randomizer.Next(0, inputCopy.Count); shuffledCards.Add(inputCopy[pos]); inputCopy.RemoveAt(pos); } return(shuffledCards); }
public static GeneticOperation DetermineGeneticOperation(int crossoverChance, int mutationChance, IRandomizer r) { var num = r.Next(1, 101); const int crossoverLowerBound = 1; var crossoverUpperBound = crossoverChance; var mutationLowerBound = crossoverUpperBound + 1; var mutationUpperBound = mutationLowerBound + (mutationChance - 1); if (num >= crossoverLowerBound && num <= crossoverUpperBound) { return(GeneticOperation.Crossover); } if (num >= mutationLowerBound && num <= mutationUpperBound) { return(GeneticOperation.Mutation); } return(GeneticOperation.Reproduction); }
public static IEnumerable <T> Shuffle <T>(this IList <T> collection, IRandomizer provider, int itemsToTake) { // make a copy of the argument IList <T> safeCollection = collection == null ? new List <T>(0) : collection.ToList(); // take a copy of the current list int initialCount = Math.Min(safeCollection.Count, itemsToTake); if (safeCollection.Count > 0) { // iterate count times and "randomly" return one of the // elements for (int i = 1; i <= initialCount; i++) { // maximum index actually buffer.Count -1 because // Random.Next will only return values LESS than // specified. int randomIndex = provider.Next(safeCollection.Count); yield return(safeCollection[randomIndex]); // remove the element to prevent further selection safeCollection.RemoveAt(randomIndex); } } }
//This overloaded method allows us to unit test that things are shuffled without any randomness. We do this by us supplying the moq randomizer. public static IEnumerable <T> Shuffle <T, R>(this IEnumerable <T> items, IRandomizer <R> randomizer) { return(items.OrderBy(i => randomizer.Next())); }