Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?


Removed 2nd question about hack safety

share|improve this question
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. – bummzack Jan 25 '12 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... – bcrist Aug 26 at 2:56

2 Answers 2

up vote 4 down vote accepted

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: 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.

share|improve this answer
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! – ThePadawan Aug 16 '12 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.