Application of Exponential Distribution

I'm in the process of developing a traffic simulation using Discrete Event Simulation approach.

So I have the core stuff working but I need to have some sort of distribution to tell the simulation how many vehicles to be generated for instance per minute. Mathematics isn't exactly my strong side but so far I've figured that I need to implement a negative exponential distribution.

What I want is to have vehicles or let's simply call them events in this case, to be generated with some distribution which I think (correct me if I'm wrong please) I can use the exponential distribution for my needs here.

The problem I'm having (a quite big problem) is that I don't understand how it works. Researching about it seems extremely overwhelming and I end up getting confused. So I obviously need to understand it before I can implement it.

Could someone be so kind and try and explain to me how it works. I understand so far that it tells us for example if we know that there are two cars passing a certain point on a road every minute then we can calculate the probability for how long we have to wait until the next car arrives?

Also any material that you think I should read and lastly whether using the exponential distribution is suitable in this case.

-
It sounds to me like you understand the main idea. Your question as phrased is so broad that it's practically impossible to answer. Are there specific things you're confused about? –  Jonathan Christensen Jan 23 '13 at 16:21
Mr. Poisson is your friend. You may want to look at this. As @JonathanChristensen mentioned, if you want to help us, please be more specific. –  jay-sun Jan 23 '13 at 18:11
@JonathanChristensen I apologize for not making it specific. I want to be able to instruct my simulation to generate some events according to a distribution. So let's say the event is to generate cars. I want to generate about 20 cars a minute. Then how do I use exponential distribution to distribute these 20 cars in that given minute? I'm not sure how to use the distribution. What will the distribution tell me? The "space" between the different events being generated? I don't know how to mathematically work this out in order for me to implement the distribution. Hope it makes sense. –  user1070241 Jan 23 '13 at 20:54
@jay-sun Thanks for the link. I will look into this. Please see the above if that helped make the question a bit more specific. Thanks –  user1070241 Jan 23 '13 at 20:54
@user1070241 Ok, answer below. –  Jonathan Christensen Jan 23 '13 at 21:11

Yes, you can use the exponential distribution to model the time between cars: set it up with the appropriate rate (2 cars/min or 20 cars/min or whatever) and then do a cumulative sum (cumsum in R) to find the time in minutes at which each car passes. Here's an example in R:

> waits <- rexp(10,2)
> waits
[1] 0.14713730 0.26549532 0.83353238 0.19284503 0.30513264 0.62242778
[7] 0.01943296 0.25699842 0.40801597 0.31635216
> cumsum(waits)
[1] 0.1471373 0.4126326 1.2461650 1.4390100 1.7441427 2.3665704 2.3860034
[8] 2.6430018 3.0510178 3.3673699


Here we have an average of two cars per minute. The first comes at about .15 minutes, the second at .26 minutes after that (i.e., at .41 minutes), and so on. The tenth one comes at 3.36 minutes.

There's another way that doesn't require doing the cumulative sum, though, which may be easier: a Poisson process, which directly generates the times at which cars come. To do it this way, you first sample the total number of cars $n$ that you see in the time interval from a Poisson distribution with parameter Rate*Time (e.g. if you have 2 cars per minute, and your time interval is an hour, your parameter will be 2*60 = 120), and then sample $n$ points from a uniform distribution on the time interval. The result ends up being the same either way. If you want, you can then calculate the waiting times (what you sampled from an Exponential distribution using the first method) by taking the differences between the ordered samples (diff in R will do this for you).

Here's an example of the alternative approach:

> n <- rpois(10,2*5)
> runif(n,0,5)
[1] 4.3983792 2.0030962 3.6927402 1.5854187 0.3782581 2.0634806 1.0005454
[8] 0.1659071 2.4213297 3.5768906
> diff(c(0,sort(a)))
[1] 0.16590711 0.21235101 0.62228724 0.58487331 0.41767751 0.06038446
[7] 0.35784905 1.15556095 0.11584959 0.70563900


Here, we first sampled $n$, which turned out to be nine, so we're simulating seeing 9 cars in the first 5 minutes (2 cars per minute average). Then we sample the times uniformly from 0 to 5, getting the vector of times. Note that they aren't in order. If we want to get the waiting times back, then we can run the line of code beginning with diff.

-
Thank you Jonathan. This is exactly the information that I was looking for! –  user1070241 Jan 23 '13 at 21:22
Sorry to bother you again. I've just tried to run the above examples through R using RGui. Everytime I use rexp(10,2) I get different results. I obviously want this to happen and I understand why it should happen but I don't understand HOW it can derive at different results each time. Can you please explain this? –  user1070241 Jan 23 '13 at 22:06
It's a random number generator. That's what it does. If you want the same results, use set.seed() with some number inside the parentheses before you run it every time. If you want theory on random number generation, I expect Wikipedia has some... –  Jonathan Christensen Jan 23 '13 at 22:08