/// <summary> /// Generates a random point within this box. /// </summary> /// <param name="rand"></param> /// <returns></returns> public GeoCoordinate GenerateRandomIn(IRandomGenerator rand) { double lat = (double)rand.Generate(1.0) * this.DeltaLat; double lon = (double)rand.Generate(1.0) * this.DeltaLon; return(new GeoCoordinate(this.MinLat + lat, this.MinLon + lon)); }
/// <summary> /// Offsets this coordinate in a random direction. /// </summary> /// <param name="randomGenerator"></param> /// <param name="meter"></param> /// <returns></returns> public GeoCoordinate OffsetRandom(IRandomGenerator randomGenerator, Meter meter) { GeoCoordinate offsetCoordinate = this.OffsetWithDistances(meter.Value / System.Math.Sqrt(2)); double offsetLat = offsetCoordinate.Latitude - this.Latitude; double offsetLon = offsetCoordinate.Longitude - this.Longitude; offsetLat = (1.0 - randomGenerator.Generate(2.0)) * offsetLat; offsetLon = (1.0 - randomGenerator.Generate(2.0)) * offsetLon; return(new GeoCoordinate(this.Latitude + offsetLat, this.Longitude + offsetLon)); }
public (int, int) GetStartAndEndIndexes(IRandomGenerator random, int gridsize) { List <int> edgeIndexes = GetGridEdgeIndexes(gridsize); int startIndex = edgeIndexes[random.Generate(0, edgeIndexes.Count - 1)]; // Select a random index on the EDGE of the maze as the start room. int endIndex = random.Generate(0, gridsize * gridsize - 1); // Select a random index anywhere in the maze as the end room. if (startIndex == endIndex) { endIndex = (endIndex + 1) % (gridsize * gridsize); // Move end index up by 1, with overflow protection } return(startIndex, endIndex); }
public (int, int) Roll() { int roll1 = RandomGenerator.Generate(NumSides); int roll2 = RandomGenerator.Generate(NumSides); if (roll1 == roll2) { LastRollWasDoubles = true; } if (roll1 != roll2) { LastRollWasDoubles = false; } return(roll1, roll2); }
public Round Run(IEnumerable <UserInGame> userGames) { if (!_gameStarted) { throw new GameNotStartedException(); } if (TotalRounds == CurrentRound) { throw new GameOverException(); } var randomNumber = _randomGenerator.Generate(_maxGuessNo); var winner = userGames .Select(u => new { Id = u.UserId, Diff = Math.Abs(u.Number - randomNumber) }) .OrderBy(a => a.Diff) .First(); CurrentRound++; RoundsLeft--; var lastRound = TotalRounds == CurrentRound; var newRound = new Round(Id, randomNumber, winner.Id, CurrentRound, lastRound); Rounds.Add(newRound); return(newRound); }
public int MakeGuess(int min, int max) { var guess = _randomGenerator.Generate(min, max, _guessedNumbers); _guessedNumbers.Add(guess); return(guess); }
protected override byte[] GenerateFileContent(int contentLength) { string generatedString = randomGenerator.Generate(contentLength); var bytes = Encoding.Unicode.GetBytes(generatedString); return(bytes); }
public GeoCoordinate OffsetRandom(IRandomGenerator randomGenerator, Meter meter) { GeoCoordinate geoCoordinate = this.OffsetWithDistances((Meter)(meter.Value / System.Math.Sqrt(2.0))); double num1 = geoCoordinate.Latitude - this.Latitude; double num2 = geoCoordinate.Longitude - this.Longitude; return(new GeoCoordinate(this.Latitude + (1.0 - randomGenerator.Generate(2.0)) * num1, this.Longitude + (1.0 - randomGenerator.Generate(2.0)) * num2)); }
public IConnectionData Connect(INeuronData origin, INeuronData destination) { var connection = new ConnectionData(ReserveId(), weightRandom.Generate(), origin.Id, destination.Id); origin.OutConnectionIds.Add(connection.Id); destination.OutConnectionIds.Add(connection.Id); return(connection); }
/// <summary> /// Checks wether the treasure hunter gets injured in a room. /// </summary> /// <param name="random">A random number generator.</param> /// <param name="roomId">ID of the room.</param> /// <returns></returns> public static bool TrapCheck(IRandomGenerator random, IMaze maze, int roomId) { bool injured = random.Generate() > maze.Rooms[roomId].BehaviourThreshold; if (injured) { maze.Rooms[roomId].Description += $" {maze.Rooms[roomId].Behaviour}"; } return(injured); }
public Task Write() { return(Task.Run(() => { while (!_canceled) { var user = _generator.Generate <User>(); _cosmosWriter.Write(user); } })); }
public T NextItem() { if (_distribution.Count == 0) { return(default(T)); } var next = _randomNumberGenerator.Generate(0, _distribution.Count); return(_distribution[next]); }
public Card[] Shuffle(Card[] deck) { for (var n = deck.Length - 1; n > 0; --n) { var rand = _generator.Generate(n + 1); var temp = deck[n]; deck[n] = deck[rand]; deck[rand] = temp; } return(deck); }
public string GetTest() { Int32 number = _firstLineGenerator.Generate(); string result = number.ToString(); StringBuilder temp = new StringBuilder(2 * number); for (int i = 0; i < number; i++) { temp.Append(_linesGenerator.Generate().ToString()); temp.Append("\n"); } return(result + "\n" + temp.ToString()); }
public static void Shuffle <T>(this IList <T> list, IRandomGenerator random) { int count = list.Count; while (count > 1) { --count; int index = random.Generate(count + 1); T obj = list[index]; list[index] = list[count]; list[count] = obj; } }
/// <summary> /// Shuffles the list using Fisher-Yates shuffle. /// </summary> /// <typeparam name="T"></typeparam> public static void Shuffle <T>(this IList <T> list, IRandomGenerator random) { var n = list.Count; while (n > 1) { n--; var k = random.Generate(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } }
private string FormatMovie(Movie movie) { var pickSentenceStyle = m_randomGenerator.Generate(0, 5); string format; switch (pickSentenceStyle) { default: { format = $"<p>{movie.Name} <break strength=\"x-strong\" time=\"850ms\" /> läuft um {{0}}.</p>"; break; } case 2: case 3: { format = $"<p>{movie.Name} <break strength=\"x-strong\" time=\"850ms\" /> wird um {{0}} gezeigt.</p>"; break; } case 4: { format = $"<p>{movie.Name} <break strength=\"x-strong\" time=\"850ms\" /> beginnt um {{0}}.</p>"; break; } case 5: { format = $"<p>{movie.Name} <break strength=\"x-strong\" time=\"850ms\" /> startet um {{0}}.</p>"; break; } } var showTimes = movie.Vorstellungen.OrderBy(p => p.VorstellungTime) .Select(p => MarkAsTime(p.VorstellungTime.ToString("hh\\:mm"))).ToList(); string times; if (showTimes.Count > 1) { times = string.Format($"{string.Join(", ", showTimes.Take(showTimes.Count - 1))} und {showTimes.Last()}"); } else { times = showTimes[0]; } return(string.Format(format, times)); }
/// <summary> /// 获取文件名 /// </summary> /// <param name="fileName">文件名</param> /// <returns></returns> private string GetFileName(string fileName) { var name = Path.GetFileNameWithoutExtension(fileName); var extension = Path.GetExtension(fileName).TrimStart('.'); if (string.IsNullOrWhiteSpace(extension)) { extension = fileName; name = string.Empty; } if (string.IsNullOrWhiteSpace(name)) { name = _randomGenerator.Generate(); } return($"{name}.{extension}"); }
private void GenerateFood() { Point genPoint; do { genPoint = _randomGenerator.Generate(); if (_emptyField.Count(point => point == genPoint) == 0) { if (!_playerSnake.CheckingCollisions(genPoint, 0)) { Food = genPoint; } } } while (Food != genPoint); }
/// <summary> /// Executes the mutation. /// </summary> /// <param name="solver"></param> /// <param name="mutating"></param> /// <returns></returns> public virtual Individual <GenomeType, ProblemType, WeightType> Mutate( Solver <GenomeType, ProblemType, WeightType> solver, Individual <GenomeType, ProblemType, WeightType> mutating) { double val = _generator.Generate(1.0); double prob = 0; for (int idx = 0; idx < _probabilities.Count; idx++) { prob = prob + _probabilities[idx]; if (prob > val) { return(_operations[idx].Mutate(solver, mutating)); } } throw new Exception("invalid probabilities!"); }
public int RollDice() { var progress = _randomGenerator.Generate(); if (_currentProgress[_currentPlayer] + progress == 100) { _currentProgress[_currentPlayer] += progress; Finished = true; Winner = _currentPlayer; } else if (_currentProgress[_currentPlayer] + progress < 100) { _currentProgress[_currentPlayer] += progress; } return(progress); }
public RoomType GetRandomSafeRoomType(IRandomGenerator random) { IRoomFactory rf = new RoomFactory(); RoomType[] roomTypes = (RoomType[])Enum.GetValues(typeof(RoomType)); List <RoomType> safeTypes = new List <RoomType>(); foreach (var type in roomTypes) { var room = rf.Create(type); if (room.BehaviourThreshold == 1) { safeTypes.Add(type); } } return(safeTypes[random.Generate(0, safeTypes.Count)]); }
private void GenerateFood() { Point genPoint; while (true) { genPoint = _randomGenerator.Generate(); if (_emptyField.Count(point => point.X == genPoint.X && point.Y == genPoint.Y) == 0) { if (_playerSnake.Body.Count(point => point.X == genPoint.X && point.Y == genPoint.Y) == 0) { Food = genPoint; return; } } } }
private async Task PublishRandom() { try { string randomMessage = await randomGenerator.Generate(); SystemMessageEvent m = new SystemMessageEvent { Created = DateTime.UtcNow, Node = Context.NodeContext.NodeName, Service = Context.ServiceName.ToString().Split("/").Last(), Message = randomMessage }; ServiceEventSource.Current.ServiceMessage(Context, $"Publishing message: {nameof(SystemMessageEvent)}"); await messagePublisher.PublishMessageAsync(m); } catch (Exception e) { ServiceEventSource.Current.ServiceMessage(Context, e.Message); } }
public (IRoom[], int, int) GenerateMaze(IRandomGenerator random, int gridsize) { IRoom[] rooms = new IRoom[gridsize * gridsize]; IRoomFactory rf = new RoomFactory(); var(startIndex, endIndex) = GetStartAndEndIndexes(random, gridsize); rooms[startIndex] = rf.Create(GetRandomSafeRoomType(random)); rooms[endIndex] = rf.Create(GetRandomSafeRoomType(random)); // List of all roomtypes. RoomType[] roomTypes = (RoomType[])Enum.GetValues(typeof(RoomType)); for (int i = 0; i < rooms.Length; i++) { if (i == startIndex || i == endIndex) { continue; } rooms[i] = rf.Create(roomTypes[random.Generate(0, roomTypes.Length)]); // Place a room of random type on each index. } return(rooms, startIndex, endIndex); }
/// <summary> /// Offsets this coordinate in a random direction. /// </summary> /// <param name="randomGenerator"></param> /// <param name="meter"></param> /// <returns></returns> public GeoCoordinate OffsetRandom(IRandomGenerator randomGenerator, Meter meter) { GeoCoordinate offsetCoordinate = this.OffsetWithDistances(meter.Value / System.Math.Sqrt(2)); double offsetLat = offsetCoordinate.Latitude - this.Latitude; double offsetLon = offsetCoordinate.Longitude - this.Longitude; offsetLat = (1.0 - randomGenerator.Generate(2.0)) * offsetLat; offsetLon = (1.0 - randomGenerator.Generate(2.0)) * offsetLon; return new GeoCoordinate(this.Latitude + offsetLat, this.Longitude + offsetLon); }
public int GenerateRandomNumber() { return(_generator.Generate()); }
public Guid GetRandomDataNodeId() { return(_dataNodes.Keys.ElementAt(_randomGenerator.Generate(_dataNodes.Count()))); }
public string GetTest() { return(_generatorFirstObject.Generate() + " " + _generatorSecondObject.Generate().ToString()); }
public GeoCoordinate GenerateRandomIn(IRandomGenerator rand) { return(new GeoCoordinate(this.MinLat + rand.Generate(1.0) * this.DeltaLat, this.MinLon + rand.Generate(1.0) * this.DeltaLon)); }
/// <summary> /// Generates a random point within this box. /// </summary> /// <param name="rand"></param> /// <returns></returns> public GeoCoordinate GenerateRandomIn(IRandomGenerator rand) { double lat = (double)rand.Generate(1.0) * this.DeltaLat; double lon = (double)rand.Generate(1.0) * this.DeltaLon; return new GeoCoordinate(this.MinLat + lat, this.MinLon + lon); }
public static string PickRandom(string[] Input, IRandomGenerator RandomGenerator) { return(Input[RandomGenerator.Generate(Input.Length - 1)]); }