public T GetRandomWeightedItem <T>(T[] items) where T : class, IWeightedItem { if (items == null || items.Length == 0) { throw new ArgumentException($"{nameof(items)} is null or empty."); } if (items.Length == 1) { return(items[0]); } var totalWeight = items.Sum(x => x.RandomizationWeight); var randomValue = _randomNumberGenerator.GetRandomInteger(totalWeight); foreach (var item in items) { randomValue -= item.RandomizationWeight; if (randomValue <= 0) { return(item); } } throw new InvalidOperationException("Error trying to determine random item."); }
public override void StartGame() { numberToGuess = randomNumberGenerator.GetRandomInteger(30); IsRunning = true; IsWon = false; }
public string GetRandomWord() { int wordCount = wordListLoader.WordList.Count; int randomWordPosition = randomNumberGenerator.GetRandomInteger(wordCount); return(wordListLoader.WordList[randomWordPosition]); }
public TColor GetRandomColor <TColor>() where TColor : Enum { int numberOfPossibleColors = Enum.GetNames(typeof(TColor)).Length; int randomNumber = randomGenerator.GetRandomInteger(numberOfPossibleColors); return((TColor)Enum.ToObject(typeof(TColor), randomNumber)); }
public async Task <SettlementResultsModel> GenerateSettlement(string settlementCode, IBuildingNameGenerator buildingNameGenerator) { var settlement = await _repo.GetSettlement(settlementCode); if (settlement == null) { throw new ArgumentException($"{settlementCode} is an invalid {nameof(settlementCode)}"); } var buildingNames = await buildingNameGenerator.ListBuildingTypes(); var results = CreateResultsModel(settlement); foreach (var settlementBuilding in settlement.Buildings) { var buildingType = buildingNames.FirstOrDefault(x => x.Id == settlementBuilding.BuildingTypeId); results.Buildings.Add(buildingType, new List <BuildingNameResultModel>()); var buildingList = results.Buildings[buildingType]; var numberToMake = _randomNumber.GetRandomInteger(settlementBuilding.MinBuildings, settlementBuilding.MaxBuildings); for (int i = 0; i < numberToMake; i++) { var building = await buildingNameGenerator.GenerateBuildingName(buildingType.Code); if (buildingList.Any(x => x.BuildingName == building.BuildingName)) { i--; } else { buildingList.Add(building); } } } return(results); }
private string CreateNameFromParts(CharacterNameResultModel.CharacterNameDefinition[] nameParts, int seperatorChance, string[] seperators) { var name = new StringBuilder(); var adjustedChance = seperatorChance % 101; for (var i = 0; i < nameParts.Length; i++) { var namePart = nameParts[i]; name.Append(namePart.NamePart); if (i < nameParts.Length - 1) { var nextPart = nameParts[i + 1]; if (AncestryLiterals.Literals.Contains(namePart.NamePartCode) || AncestryLiterals.Literals.Contains(nextPart.NamePartCode)) { continue; } var randomNumber = _randomNumberGenerator.GetRandomInteger(100); if (randomNumber > adjustedChance) { continue; } var seperator = _randomItemSelector.GetRandomItem(seperators); if (namePart.NamePart.EndsWith(seperator, true, null) || nextPart.NamePart.StartsWith(seperator, true, null)) { continue; } name.Append(seperator); } } var textInfo = new CultureInfo("en-US", false).TextInfo; return(textInfo.ToTitleCase(name.ToString().ToLower())); }