public Drop SelectDropRandomly() { //For Unit Test if description is empty drop nothing if (drops.Length == 0) { return(null); } for (int i = 0; i < drops.Length; i++) { //select an random item out of array, so that the drop rates are correct and the order doesn't matter int rndIndex = Random.Range(0, drops.Length); float rnd = Random.value; DropProbabilityPair pair = drops[rndIndex]; if (rnd < pair.Probability) { return(pair.Drop); } } //to an ensure that always omething is droped -> drop an random item out of array return(drops[Random.Range(0, drops.Length)].Drop); }
public Drop SelectDropRandomly() { float total = 0; // sum up drop probabilities for (int i = 0; i < drops.Length; i++) { DropProbabilityPair pair = drops[i]; total += pair.Probability; } float rnd = Random.Range(0, total); float curr = 0; for (int i = 0; i < drops.Length; i++) { DropProbabilityPair pair = drops[i]; if (rnd < (curr + pair.Probability)) { return(pair.Drop); } else { curr += pair.Probability; } } return(null); }
public void TestDropHalfHalfDistribution() { var lootTable = ScriptableObject.CreateInstance <LootDescription>(); var dropOne = ScriptableObject.CreateInstance <Drop>(); var dropTwo = ScriptableObject.CreateInstance <Drop>(); var pairOne = new DropProbabilityPair(); pairOne.Drop = dropOne; pairOne.Probability = .5f; var pairTwo = new DropProbabilityPair(); pairTwo.Drop = dropTwo; pairTwo.Probability = .5f; DropProbabilityPair[] pair = { pairOne, pairTwo }; lootTable.SetDrops(pair); for (int i = 0; i < 100; i++) { Drop drop = lootTable.SelectDropRandomly(); Assert.IsNotNull(drop); } }
public void TestThatOrderIsIrrelevant() { var lootTable = ScriptableObject.CreateInstance <LootDescription>(); var lootTableSwitched = ScriptableObject.CreateInstance <LootDescription>(); var dropOne = ScriptableObject.CreateInstance <Drop>(); var dropTwo = ScriptableObject.CreateInstance <Drop>(); dropOne.DropName = "One"; dropTwo.DropName = "Two"; var pairOne = new DropProbabilityPair(); pairOne.Drop = dropOne; pairOne.Probability = .1f; var pairTwo = new DropProbabilityPair(); pairTwo.Drop = dropTwo; pairTwo.Probability = .9f; DropProbabilityPair[] pair = { pairOne, pairTwo }; DropProbabilityPair[] pairSwitched = { pairTwo, pairOne }; lootTable.SetDrops(pair); lootTableSwitched.SetDrops(pairSwitched); Drop[] resultOne = new Drop[1000]; Drop[] resultSwitched = new Drop[1000]; for (int i = 0; i < 1000; i++) { resultOne[i] = lootTable.SelectDropRandomly(); resultSwitched[i] = lootTableSwitched.SelectDropRandomly(); } int rOneCount = 0, rSwitchedCount = 0; foreach (Drop drop in resultOne) { if (drop.DropName == "One") { rOneCount++; } } foreach (Drop drop in resultSwitched) { if (drop.DropName == "One") { rSwitchedCount++; } } Assert.GreaterOrEqual(rOneCount, 80); Assert.GreaterOrEqual(rSwitchedCount, 80); Assert.LessOrEqual(rOneCount, 130); Assert.LessOrEqual(rSwitchedCount, 130); }
public Drop SelectDropRandomly() { for (int i = 0; i < drops.Length; i++) { float rnd = Random.value; DropProbabilityPair pair = drops[i]; if (rnd < pair.Probability) { return(pair.Drop); } } return(null); }