Sign in to follow this  
Spac3Rat

Calculating the chance of an event happening

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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%).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ouch... That did it.

I'm getting some fluctuations but they are well within the limits I need. I keep forgetting not to put the random object creation in the loop cycles. Thank you SiCrane.

And thank you to the guys that posted here too. I appreciate your time and your willing to help a fellow coder.

Thank you all.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
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.


Of course, in this case the events are not independent, so a player can predict what will happen next with better than 90% probability after he has observed a few rounds. The obvious extreme case is just before the last round: If the player has been counting, then he can correctly predict what will happen next with 100% probability.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this