- Home /
Weighted Random Spawning
I am working on a word game that I have made a bunch of letter prefabs for. Each prefab has a public float frequency variable. Where the "A" prefab frequency is 8.167 and the "Z" frequency is .074. How would I build a spawner that would instantiate prefabs randomly, but do so in such a way that A is more likely than Z based on the frequency variable?
Thanks,
Jason
http://wiki.unity3d.com/index.php/WeightedRandomizer First page that showed up from typing "Weighted random spawning"
Answer by doublemax · Jan 18, 2017 at 09:30 PM
Add the two values together. Create a random number between 0 and the sum. If it's in the range [0-A], choose A, if it's in range [A - A+Z], choose Z.
Assu$$anonymous$$g that A, B, C, .., Z that each frequency gets smaller and smaller. Wouldn't it be choose A if the random number is between 0 and A, choose B if the random number is between A and A + B, choose C if the random number is between A + B and A + B + C, ..., and choose Z if the random number is between A + B + C + .. + Y and A + B + C + ... + Y + Z ??
What i wrote was for the case that you only have 2 values, A and Z. I didn't realize you meant a range of A-Z. Your interpretation is correct though.