Updated for clarity
I am making a game and created a JSON config file where I can configure settings such as the kinds of loot that will drop for any given level. I also configured (in a JSON array of loot) the frequency for each item. I want a coin to drop 30% of the time and a heart to drop 50% of the time for level 1. So I configure coin to have a frequency of 0.3, and heart to have a frequency of 0.5. Not all loot drops on each level (some may have a frequency of 0.0)
How can I ensure a coin drops 0.3 (or 30%) of the time and a heart to drop 0.5 or 50% of the time when I have my ObjectFactory spawn a given piece of loot? I understand I can generate a number between 1 and 100 or 0.0 and 1.0. But then what?
If my random number generates 0.7, what does that mean? Do I spawn the coin, or the heart?
Do I have to literally do an if statement like this?
//30 and 80 came from the JSON file
if (rngNumber < 30)
//spawn coin
else if (rngNumber >= 30 && < 80)
//spawn heart
else
//spawn nothing
See the problem with that, then, is that as I go from level to level, that doesn't work. Because if I changed the heart to be 90% on level 2, then the code looks like this:
if (rngNumber <= 89)
//spawn heart
else
//do nothing
see what I mean? I want a way to abstractly take the frequency numbers present in the JSON Config, and spawn items according to their frequency, without having to consider upper/lower bounds in a series of if..else statements.
I feel like I would need to define a range for each piece of loot, and then check if a number falls within that particular range. That's the best I can think of. Thoughts?
Thanks!