Quote:
Originally Posted by Freaksken
Lets say you have chance of 5 in a 1000 to get a certain item, so 0.5%.
Take a random number between 1 and 1000 (both included).
If this number exceeds a certain threshold (1000 - 5 = 995 in this case), the item is given to the player.
Edit: prinside was faster to reply, basically what he said
|
5 in 1000 is 0.005% not 0.5%, 0.5% is 50% lol
doing if(random(1000) < 50) would be 5% chance
Quote:
Originally Posted by prineside
1. Create an array of that things, for example:
PHP код:
{
{ int thingOne, int probablity },
{ int thingTwo, int probablity },
...
}
2. Sum all probablities
3. Get random number RAND from 0 to sum probablity
4. Go through array and accumulate probablities
5. When probablity is bigger than RAND, give last thing in array to player
Probablity is an integer. The bigger it is, the bigger is the probablity of getting this item in random
For example:
{ MONEY_1000, 5 },
{ SCORE_1000000, 1 },
{ WEAPON_DEAGLE, 3 }
|
So basically, instead of having a 2d array, i would do a 3d array.
{ type, out_of, prob }
Where type = what to give them (such as a function that contains a switch that will give them a reward based on the id). out_of = the total number such as 1000, and prob = 5 for 5 out of 1000.
This way you would be able to have indexed types, along with different percentages.
You could also do something like,
{ type, 50000, 1 } or any other combination