/// <summary> /// Simulates a round of scavenging and returns a list of scavenged items. /// items currently represented by the Part class; /// </summary> public Part Scavenge(LootTable table) { // Decide which rarity to choose from var rarityWeighted = new WeightedSet <Rarity>(EnumUtils.GetValues <Rarity>(), table.rarityWeighting); var rarity = RandomU.instance.Choice(rarityWeighted); // Initialize the drops to the items and weights from the correct rarity // Would be a dictionry if I had the time to write a good GUI for it WeightedSet <Part> drops; switch (rarity) { case Rarity.Common: drops = new WeightedSet <Part>(table.itemsCommon, table.itemsCommonWeights); break; case Rarity.Uncommon: drops = new WeightedSet <Part>(table.itemsUncommon, table.itemsUncommonWeights); break; case Rarity.Rare: drops = new WeightedSet <Part>(table.itemsRare, table.itemsRareWeights); break; default: return(null); } //// Make parts you already have less likely //drops.ApplyMetric((p) => Player.instance.inventory.Contains(p) ? 0 : 1); // Return a random sample from the drops table return(RandomU.instance.Choice(drops)); }
static public void WeightedSetTest() { System.Random r = new System.Random(600); WeightedSet <char> set = new WeightedSet <char>().Add('a', 3).Add('b', 4).Add('c', 5).Add('d', 6); set.Remove('b'); Assert.AreEqual(0, set.GetWeight('b')); Assert.AreEqual(5, set.GetWeight('c')); set.SetWeight('c', 7); Assert.AreEqual(7, set.GetWeight('c')); float newWeight = set.ChangeWeight('d', -10); Assert.AreEqual(0, newWeight); Assert.AreEqual(0, set.GetWeight('d')); set.ChangeWeight('d', 15); Assert.AreEqual(15, set.GetWeight('d')); set.ChangeWeight('a', 10); Assert.AreEqual(13, set.GetWeight('a')); int a = 0, b = 0, c = 0, d = 0; int iter = 100000; for (int i = 0; i < iter; ++i) { switch (r.Choose(set)) { case 'a': ++a; break; case 'b': ++b; break; case 'c': ++c; break; case 'd': ++d; break; } } Debug.LogFormat("a={0}%, b={1}%, c={2}%, d={3}%, ({4} samples)", 100f * a / iter, 100f * b / iter, 100f * c / iter, 100f * d / iter, iter); }
public static T SelectRandom(T[] from) { float total = WeightedSet <T> .GetTotalWeight(from); int index = WeightedSet <T> .SelectRandomIndex(from, total); if (index == -1) { return(default(T)); } return(from[index]); }
public static void TestHalf() { WeightedSet <int> set = new WeightedSet <int>(); set.Add(0, 0.5f); set.Add(1, 0.5f); float result = (float)Enumerable.Range(1, 100000).Select(i => set.RandomTake()).Average(); UnityEngine.Debug.Log($"result = {result}"); Assert.LessOrEqual(Math.Abs(result - 0.5f), 0.01f); }
public GameObject[] SpawnInstances(SceneScroller.SpawnableInterestInfo[] templates, float spawnChance, int amountMultiplier) { var result = new List<GameObject>(templates.Length); while (result.Count < this.amount * amountMultiplier) { if (UnityEngine.Random.Range(0.0f, 1.0f) > spawnChance) { continue; } var template = WeightedSet<SceneScroller.SpawnableInterestInfo>.SelectRandom(templates); result.Add(this.SpawnInstance(template.spawnableInterest)); } return result.ToArray(); }
public static int[][] RandomTree_Edges(Random rnd, int verticesCount, int maxChildCount, bool doPermutation = true) { var vertices = new int[verticesCount]; for (var i = 0; i < verticesCount; i++) { vertices[i] = i; } var edges = new List <int[]>(); var childCount = new int[verticesCount]; var added = new WeightedSet <int>(); added.Add(0, 1d); for (var i = 1; i < verticesCount; i++) { if (added.Count == 0) { break; } var parent = added.RandomElement(rnd); edges.Add(new[] { parent, i }); childCount[parent]++; added.Add(i, 1d); added.MultiplyWeight(parent, 0.5); if (childCount[parent] == maxChildCount) { added.Remove(parent); } } if (doPermutation) { var permutation = vertices.ToArray().Shuffle(rnd); foreach (var edge in edges) { edge[0] = permutation[edge[0]]; edge[1] = permutation[edge[1]]; } } return(edges.ToArray()); }
private void LoadSet(IReadOnlyList <string> values, List <TreeNode> treeNodes) { if (string.IsNullOrEmpty(values[3]) || string.IsNullOrEmpty(values[4])) { return; } // this is a weighted exercise ICategory category; if (!m_viewModel.Categories.TryGetValue(values[2], out category)) { category = new Category(values[2]); m_viewModel.Categories[category.Name] = category; treeNodes.Add(new TreeNode(category.Name) { Tag = category }); } IExercise exercise; if (!category.Exercises.TryGetValue(values[1], out exercise)) { exercise = new Exercise(values[1], category); category.Exercises[exercise.Name] = exercise; var parentNode = treeNodes.Find(x => x.Text == category.Name); if (parentNode == null) { throw new Exception( $"could not find category parent node for exercise: {exercise.Name}"); } parentNode.Nodes.Add(new TreeNode(exercise.Name) { Tag = exercise }); } var date = DateTime.Parse(values[0]); var weight = double.Parse(values[3]); var reps = int.Parse(values[4]); var set = new WeightedSet(date, weight, reps); exercise.Sets[set.Date] = set; }
protected SceneGroupInstanceInfo GetNextSceneGroupInstanceInfo() { SceneGroupInstanceInfo result = new SceneGroupInstanceInfo(); if (this.overrideSceneGroupInfo.sceneGroup != null) { result.group = this.overrideSceneGroupInfo; } else { result.group = WeightedSet <SceneGroupInfo> .SelectRandom(this.sceneGroupInfos); } var instanceCount = result.group.sceneInstanceCount; result.remainingInstanceCount = instanceCount.GetRandomValue(); return(result); }
public static int SelectRandomIndex(T[] from) { float total = WeightedSet <T> .GetTotalWeight(from); return(WeightedSet <T> .SelectRandomIndex(from, total)); }
public EdgeTypeWeighted() { weightedSet = new WeightedSet<Reference>(SortDirection.Desc); }
public static IEnumerable <T> Random <T>(this IEnumerable <T> enumerable, int count) { IEnumerable <T> weightedSet = new WeightedSet <T>(enumerable).RandomItems; return(weightedSet.Take(count)); }