# Designing Drops system - How and where chance of drops are defined?

Just found a related topic but I have different/extended questions

I was wondering how the implement the drop system. I have to start designing the DB model for this so I was wondering whether what I am thinking is a good practice or not.

My game is like "City building" kind of browser based game (Flex/Flash app) with PHP and MySQL in the backend. In my game, like in any other games, there are chance based drop rate. I want to design the drop system such a way that I can easily configure

• What item(s) can be dropped from which game element
• What is the percent chance (probability) of getting a drop (a 100% meaning there will be some drop always)
• What percent chance of getting a specific drop. Here I define for each item (if can drop various items) what is the chace of drop, 90% to very frequent, 5% to very rare

I am wondering how the probability is determined? It might sound very noobish but just want to confirm does it mean roll a random number and compare with the odds we defined? If I am not wrong this is good for determining "how much to drop" but not for "what to drop"

Example: If we say drop 1-10 coins or 20-50 coins. I will simply call a random() function and generate a random numbed from the desired range.

Now for other example: Drop one of Bullets (95%), Empty Gun (4%), Loaded Gun (1%)

If I roll a random number range from 1-100 and compare with probability I defined

so, a number between 1-95 drops bullet, and a number between 96-99 drops empty gun and a loaded gun when it generates 100?

Is this how it has to be done? or is there a better way?

Update:

Removed 2nd question about hack safety

• You shouldn't ask multiple questions in one question. Protecting your game from hacks has been answered here. As for the random drops with different probabilities, this has been answered in this question. Jan 25, 2012 at 8:56
• As someone who works full time on the backends for citybuilder games, the thought of doing it in PHP makes me cringe... Aug 26, 2015 at 2:56
• Possible duplicate of How do I create a weighted collection and then pick a random element from it? Aug 20, 2018 at 20:16
• this was asked 6.7 years ago Aug 31, 2018 at 13:59

Let me try to go through all your questions in turn.

1. Checking whether or not a drop occurs given a certain drop chance

Just use a pseudo-random number generator and compare the result with the drop chance, just as you suggested in your question. For example, if you specify the drop chance in percentage points with up to two decimal places, you could ask

rand = generatePseudoRandomNumber(0, 10000) // between 0 and 10,000
if(rand <= dropChance * 100)


2. Checking which events happens given a set of possible events

Try to use a data structure that is more flexible than hard-coded switch expressions. I coded a basic version of a Lottery class here: https://gist.github.com/3368046. Lottery allows you to do stuff like this:

$Lottery = new Lottery();$Lottery->addEntry($item1,$dropChanceOfItem1);
$Lottery->addEntry($item2, $dropChanceOfItem2);$Lottery->addEntry($item3,$dropChanceOfItem3);
$drop =$Lottery->getWinner();


With my implementation, the drop chances do not even have to add up to 100%; the lottery determines the winner based on the number of lots that each participant has in relation to the other participants of the lottery.

3. When to check for drops and how to communicate the result to the player

I'd decide based on what the most critical bottleneck is: If bandwidth is expensive and if your service has a good latency (Amazon?), go for variant 1. If your service has latency issues, go for 2.

• Referring to @BerndBrot's numbering: To point 2., I would also refer to that functionality as an "unfair/weighted die roll". For a real-life example, check out how Boost.Random handles it! Aug 16, 2012 at 11:58

Yes, you take each percentage and map it to one or more numbers in that range. E.g. if there's 50% chance of getting a killer rabbit, and 20% chance of getting John Lennon glasses, and 30% of getting chainsaw fuel, then you would have 1...50 be the range for the rabbit, 51...70 for the glasses, and 71...100 for the fuel.

Now you generate a random number (e.g. using rand() or whatever your language offers, limiting it to 1...100 using 1 +(rand() % (100-1)), and depending on what range it falls in, you know what to drop.

Now this only gives you a single item. To get several items, you just draw several times*. So you'd be guaranteed to get one of the three above items from draw one, and one of four other smaller items from draw 2. You can even have draws where one of the items that can drop is "nothing". So to have a 2% chance of this dropping the Super Mega Ultra Grooveshark(tm), you'd have range 1-2 be the shark, and 3 to 100 "nothing" and just do an extra draw (generate another random number between 1 and 100) with that.

So your data structure would probably be a list of draws containing a list of items and their percentages, one of which could be a "nothing" item. Then your items (e.g. enemies) would simply reference one of those data structures as their "drop this when I die" structure.

*) Of course, if you want to always drop two items together, you may need to define some sort of "box" item that just contains those to items, then just draw once to get those two.

I'm doing something like this to generate and drop random items.

1. There are 6 main categories.
2. Each category has more than ten items with drop probability percentages.
3. There are 2 attributes that determine the drop system: exploration and intelligence.

This is an example:

If exploration < 30

- category1 0-1 //0= no item. 1= 1 item
- category2 0-1
...


Then, a random number for each category (in case the result was 1) to determinate what item to drop (0-100)

and the last step:

if intelligence < 20

- category1 0-4 //quantity of the item selected.
- category2 0-4
- category3 0-4
...


in summary, with that "system" the player can get or not-get an item for each category, and a random quantity of each item they do get.