Last active
November 17, 2020 15:59
-
-
Save leducanhh/b76be78b4681fd18cedb0095fb9dfa33 to your computer and use it in GitHub Desktop.
Better random
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
float GreaterRandom(float[] lttRate) | |
{ | |
float total = 0; | |
foreach (float elem in lttRate) | |
{ | |
total += elem; | |
} | |
float randomPoint = Random.value * total; | |
for (int i = 0; i < lttRate.Length; i++) | |
{ | |
if (randomPoint < lttRate[i]) | |
{ | |
return i; | |
} | |
else | |
{ | |
randomPoint -= lttRate[i]; | |
} | |
} | |
return lttRate.Length - 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment