Created
June 28, 2018 16:54
-
-
Save xdegtyarev/b08cce3711626cf26ea1266785a1bfee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WeightedSettings{ | |
public string key; | |
public float weight; | |
public float reward; | |
} | |
public static string WeightedRandom(this List<WeightedSettings> list){ | |
var totalChance = 0f; | |
foreach(var o in list){ | |
totalChance += o.weight; | |
} | |
var seed = UnityEngine.Random.Range(0f, totalChance); | |
foreach(var o in list){ | |
seed -= o.weight; | |
if(seed<=0f){ | |
return o.key; | |
} | |
} | |
return null; | |
} | |
public static List<string> WeightedRandomSet(this List<WeightedSettings> list, int min, int max){ | |
var randomSet = new List<string>(); | |
int setLength = UnityEngine.Random.Range(min, max); | |
for(int i = 0; i<setLength; i++){ | |
randomSet.Add(list.WeightedRandom()); | |
} | |
return randomSet; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment