-3

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!

NullHypothesis
  • 4,118
  • 5
  • 34
  • 73
  • Can you give more specifics? It is hard to understand what algorithm you want with the perquisites given – Benjamin Lowry Feb 09 '16 at 13:54
  • 3
    Typically people think they are confused on HOW to achieve something when really they are really confused about WHAT they want. Seems to me this is the case here, and this is where you want to get clarity first. – Bradley Thomas Feb 09 '16 at 13:59
  • 1
    Example scenario - Drop item for 10%: Generate number <0,1.0>. If number< 0.1 then drop item else not drop item. Or where is the problem? You should clarify your intention. – Martin Makarsky Feb 09 '16 at 14:10
  • Updated for clarity - if you do have another moment would love some feedback thank you! – NullHypothesis Feb 09 '16 at 23:45

2 Answers2

1

This is how to get a random number in Swift: How does one generate a random number in Apple's Swift language?

From there, it's pretty easy to just compare the value in the file to the number that's generated. If the random number is below the threshold you drop that loot.

Community
  • 1
  • 1
Carlos
  • 5,778
  • 6
  • 44
  • 80
0

Ok this is how you do it:

First, you can't do it with a single number, you need a range. So in my config I created "Frequency" : (1, 50) meaning a number between 1 and 50, for item 1, and 51, 70 for item 2, and 71 - 100 for item 3 (you get the idea).

I then deserialized each frequency to a tuple (Int, Int)

then I just generate a random number between 1, 100 and filter like so:

 let rand = RandomInt(min: 1, max: 100)
        var loot: Loot?
        let lootData = lootConfigArray.filter{rand >= $0.frequency.0 && rand <= $0.frequency.1}.first

and this filters and returns the first item to match in the range. If the number goes out of the range it's nil, which is perfect because that means no loot dropped!

NullHypothesis
  • 4,118
  • 5
  • 34
  • 73