Calculating the chance of an event happening

Started by
10 comments, last by Emergent 15 years, 6 months ago
Hi guys. I'm a long-time reader, first time poster. I am trying to simulate the possibility of an event happening, based on the percentage of chance of it actually happening. Let me try to translate to, for example, an RPG situation. Let's say your character starts to fight another character. Your character has, for example, 10% chance of providing extra damage. So, for all the attacks your character does, the system must calculate if you do the extra damage or not. I came out with a very simple system that is giving me relatively nice results but the error margin is still too big. So big that, for a test with 10,000 cycles and a 1% chance of happening, I have 0 events. For a 10% chance, I'm getting between 9.45% and 9.47%. I really need to make it very close (less than 0.05% error). This is the system I'm using: - Get the chance % and multiply it by 10,000 (10% -> 100,000) - System choose a random number between 1 and 1,000,000) - If the number is between 1 and 100,000 (inclusive), the event happens. A practical example using other values: - 31.56% -> 315,600 - Random number between 1 and 1,000,000 - If Random number <= 315,600, event occurs. Any ideas on another algorithm or a way to improve this one? Thank you in advance. [Edited: I screwed up some values] [Edited by - Spac3Rat on October 9, 2008 10:03:05 AM]
Advertisement
I would personally generate a number between 0 and 1 (uniform distribution, double precision floating-point number), and trigger the event if that number is smaller than the probability.

So, for an event with a probability of 1%, this would trigger when the random number is smaller than 0.01.
Quote:A practical example using other values:
- 31.56% -> 315,600
- Random number between 1 and 1,000,000
- If Random number <= 316,000, event occurs.
If these are the actual numbers used, then you already have a little more than 0.1% error between 315,600 and 316,000.

Quote:For a 10% chance, I'm getting between 9.45% and 9.47%. I really need to make it very close (less than 0.05% error).

This is the system I'm using:

- Get the chance % and multiply it by 10,000 (10% -> 100,000)
- System choose a random number between 1 and 1,000,000)
- If the number is between 1 and 100,000 (inclusive), the event happens.
Another approach would be to fill an array with 1,000 positive numbers, and 9,000 zeros (this for 10,000 cycles). Then at each cycle, randomly pick one item, and remove it from the array. If you picked a positive number, deal extra damage, otherwise deal normal damage. This guarantees that you will deal extra damage in exactly 10% of cycles.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Toor I will try your approach but it seems very similar to mine (I'm using large integers instead of large float/decimal numbers).
Swift, my bad. It was 315,600 not 316,000.

I though about the array too but I don't want to have to have heavy processing for such a simple task as to calculate if an event should occur. This is something I'm doing for a website and it has to be fast and accurate.

Thank you all for jumping in. I'll start trying your approaches.

UPDATE: I just tried Toor's approach and the error is still too big.
Quote:Original post by Spac3Rat
I though about the array too but I don't want to have to have heavy processing for such a simple task as to calculate if an event should occur. This is something I'm doing for a website and it has to be fast and accurate.
A better response on my part might have been a question. Why do you need such an exacting result? A random chance is just that - random. There is a non-zero possibility that you can pick 1,000,000 random numbers and have the exact same result for every one. That is vanishingly unlikely, but expecting a fixed number of cycles to average out exactly is somewhat unlikely.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Well my main problem is that I'm getting very wild results.

While testing with 40% chance, on 1,000 tries, I got 34.2% of positives.

For chances lower than 3% I get 0 hits (and I can have chances as low as 2-3%).
I believe World of Warcraft uses ToohrVyk's method. If you aren't getting the event, either you're not seeding, your code is wrong, or you are unlucky. If you want to fix this "unlucky" situation, then you are being LESS accurate than using random chance.
How are you generating your random numbers?
I'm using VB.Net. Here's the piece of code that generates the numbers:

Dim dblChance As Double = CType(txtChance.Text.Trim, Integer) / 100
Dim objRandom As New Random(Now.Millisecond)
Dim dblN As Double = objRandom.NextDouble
If (dblN <= dblChance) Then
' Do something nice here
End If

Where txtChance is a textbox where the user enters a number from 0 to 100.

Thank you.
Do not create a new Random object every time you need to create a new random number. Create one and keep it around.

This topic is closed to new replies.

Advertisement