Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int WeightedRandom(float[] weights)
- {
- float value = Random.Range(0, weights.Sum());
- float weightSum = 0;
- for (int i = 0; i < weights.Length; i++)
- {
- weightSum += weights[i];
- if (value < weightSum)
- {
- return i;
- }
- }
- return weights.Length - 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment