Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xdegtyarev/b08cce3711626cf26ea1266785a1bfee to your computer and use it in GitHub Desktop.
Save xdegtyarev/b08cce3711626cf26ea1266785a1bfee to your computer and use it in GitHub Desktop.
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