# Continuous random chance

This topic is 3805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, In my game, I need some things to happen randomly, with probability based on some input. These need to happen continuously, rather than at fixed discrete intervals. Specifically, I have some little people walking about on a surface that will become inclined, and I want these people to lose their grip and become 'detached' if the incline gets too steep for them. So, I was thinking something along the lines of the 'probability per second', where I'd take the incline of the slope, map that to [0,1], and multiply it by the tick time. That made perfect sense up until I came to code it, when I realised that '1' may never happen if the tick time is < 1 second, and I could end up with a probability > 1 if the tick time is > 1. I know I can do some clamping and the like, but that implies that the idea isn't right and isn't likely to work the same at different framerates. So, since this is presumably a pretty standard thing to want to do in games, how do you guys go about it? Thanks in advance!

##### Share on other sites
Sounds like a Poisson process, of which radioactive decay is an example.

##### Share on other sites
Brilliant, that's exactly what I need. It'll be the non-homogenous Poisson process I guess, since the rate changes. Then the way to think of the problem is "what is the expected number of times this person would have fallen off during this tick". If it's expected to be multiple times (a long tick, or very high 'intensity'/rate), then they fall. If it's expected to be less than 1, then I suppose I either keep them on or use the value as the probability that they fall that frame. I'm not sure which is correct...

Now I've just got to get my head around the lovely mathematical notation and put it down in C++!

Cheers SnotBob!

EDIT: Hmm, looks like the equations are for finding the probability of N events occurring between two times... does that mean I'd have to check for 1, 2, 3... events each frame? If I have a particularly long frame it's possible that the most likely number of events will be large, I wouldn't want to miss that just because I only put the first few numbers into the equation... is there not some version that is for "at least N" rather than "exactly N" events occurring?

EDIT AGAIN: Thinking about it, I guess I could just get the probability for 0 events occurring and invert that, since I only care about the event occurring 0 or 1+ times.

##### Share on other sites
I guess double posting is better than triple editing :)

Looks like this can be simplified out greatly actually - the probability P that a person fell off during a frame lasting dt seconds when the 'rate' r (worked out from the incline of the terrain and whatever else I feel like chucking in), is given by:

P = 1 - e^(-r * dt)

or, if you want to use it properly, taking r2 and r1 as the rates for this and last frames:

P = 1 - e^(-(r2+r1)/2 * dt)

Putting N = 0 into the equations simplifies things greatly (the factorial and the complicated term with N in the power disappear), and since we know the rate isn't going to change dramatically between frames we can ignore the averaging with the previous frame, and since it is constant during one frame, the integration between timesteps works out as r * dt. Nice and easy :)

##### Share on other sites
Quote:
 Original post by hymermanP = 1 - e^(-r * dt)

I was just about to post you that same. It is the right formula, because
f(x)=1-e-x satisfies
f(x+y)=f(x)+f(y)-f(x)f(y)

The r parameter is of course determined by
r = - ln [1 - P(T)], where P(T) is the probability of the event occurring in the interval of duration T,
i.e. P(t) = 1 - e- r * t/T

[Edited by - SnotBob on June 15, 2008 11:42:32 AM]

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633328
• Total Posts
3011376
• ### Who's Online (See full list)

There are no registered users currently online

×