# Can i do this in ruby? a simulation process

**URL:** <https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412>\
**Category:** Ruby\
**Created:** [August 17, 2010, 11:11am UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412 "2010-08-17T11:11:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 11:11am UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/1 "2010-08-17T11:11:00Z")

</div>

still i don’t know how to begin my program on this problem:

A superhighway connects one large metropolitan area to another.  
A vehicle leaves the first city every 5-35sec. 20% of the vehicles have  
1 passenger, 30% have 2 passengers, 10% have 3 passengers and and 10%  
have 4 passengers. the remaining 30% of the vehicles are buses, which  
carry 40 people.  
It takes 50sec-1min &10sec for vehicles to travel between the two  
metropolitan  
areas. How long does it take for 500 people to arrive in the second  
city.

Requirements:

1. A simulation process of the movement of vehicles from one point to  
another.
2. The time (in minutes) for 500 people to arrive in the second city.

i can’t even think on how can i start this one in ruby a newbie like me  
have only limited knowledge on ruby. can someone help with my program?

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 11:40am UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/2 "2010-08-17T11:40:31Z")

</div>

> Requirements:
> 
> 1. A simulation process of the movement of vehicles from one point to  
> another.
> 2. The time (in minutes) for 500 people to arrive in the second city.
> 
> i can’t even think on how can i start this one in ruby a newbie like me  
> have only limited knowledge on ruby. can someone help with my program?

You sure can do it in ruby but first you have to decide what ruby have  
to do for you: take a sheet of paper and write down the differents  
steps each Vehicle has to go throught in order to reach the second  
city. I’m sure you will see more clearly what you have to do next to  
implement each step.

Hint: it have to do with probability distributions and you will most  
certainly need the metho ‘rand’

Cheers,

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 11:52am UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/3 "2010-08-17T11:52:16Z")

</div>

Jean-Julien F. wrote:

> > Requirements:
> > 
> > 1. A simulation process of the movement of vehicles from one point to  
> > another.
> > 2. The time (in minutes) for 500 people to arrive in the second city.
> > 
> > i can’t even think on how can i start this one in ruby a newbie like me  
> > have only limited knowledge on ruby. can someone help with my program?
> 
> You sure can do it in ruby but first you have to decide what ruby have  
> to do for you: take a sheet of paper and write down the differents  
> steps each Vehicle has to go throught in order to reach the second  
> city. I’m sure you will see more clearly what you have to do next to  
> implement each step.
> 
> Hint: it have to do with probability distributions and you will most  
> certainly need the metho ‘rand’
> 
> Cheers,

Cheers Thanks, anyway i already did writing and other stuff but the only  
problem is that i don’t know to start my code. ☹

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 12:01pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/4 "2010-08-17T12:01:01Z")

</div>

> Cheers Thanks, anyway i already did writing and other stuff but the only  
> problem is that i don’t know to start my code. ☹

Try to create a Vehicle class where you compute the arrival time and  
the number of passengers based on a departure time. Then make a loop  
of say 500 vehicles (to be sure everybody arrive at the end) and  
create a list of vehicles that are launched every 5-35s. Do not forget  
to sort the list based on arrival time rather than departure time.

In the end, it should give you something like

[…]  
442 passengers where there after 654 seconds  
444 passengers where there after 691 seconds  
448 passengers where there after 717 seconds  
449 passengers where there after 746 seconds  
489 passengers where there after 761 seconds  
529 passengers where there after 790 seconds

Cheers,

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 12:32pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/5 "2010-08-17T12:32:03Z")

</div>

Bruce W. wrote:

> i can’t even think on how can i start this one in ruby a newbie like me  
> have only limited knowledge on ruby. can someone help with my program?

To start with, I’d ask the following: how would you perform the  
simulation manually, for example using a pencil, some pieces of paper,  
and some dice? (You can use any imaginary dice you need, e.g. a 30-sided  
die or a 20-sided die).

I strongly suggest you do it for real, for a number of vehicles.

When you see the individual steps that you’re doing, you’ll have a  
clearer idea of the individual steps your program needs. Then you can  
come back and ask more specific questions.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 1:36pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/6 "2010-08-17T13:36:26Z")

</div>

Hmm, I may or may not disagree with you on what the output should be. I  
can’t speak for his assignment, but in real life, I would think it would  
be  
more useful to perform the simulation some number of times, printing out  
the  
number of seconds it took to get to 500 passengers each time, and the  
average of all the trials. It’s not clear to me from the description of  
the  
assignment whether the requirement is for the results of a single trial  
(rather silly) or an average.

Anyway, I did this, for fun, and consistently got an average of around  
833  
seconds on average for 100000 trials. Anyone else give this a shot? I’m  
curious to see whether different implementations give different results.  
The  
standard deviation seems to be pretty high.

On Tue, Aug 17, 2010 at 6:00 AM, Jean-Julien F. \<

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 2:25pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/7 "2010-08-17T14:25:00Z")

</div>

Andrew W. wrote:

> Anyway, I did this, for fun, and consistently got an average of around  
> 833  
> seconds on average for 100000 trials. Anyone else give this a shot? I’m  
> curious to see whether different implementations give different results.

Ruby quiz 🙂

After 10,000 trials I got an average of 803 seconds, and after a second  
10,000 trials I got 807 seconds. But I don’t think we should post any  
code until the OP has had a go.

I would certainly expect wide variation between runs, since it would be  
affected very strongly by when the buses set off. However I think the  
point of the exercise is really the simulation, because it becomes very  
simple if you treat it statistically.

(1) Average number of occupants per vehicle =  
0.2_1+0.3_2+0.1_3+0.1_4+0.3\*40 = 13.5  
(2) You can ignore the different travel times. The average rate at which  
vehicles depart must be the same as the average rate at which they  
arrive (law of conservation of vehicles)  
(3) The vehicles depart on average every 20 seconds

So you should get an average of 0.675 passengers per second, which  
implies 500 passengers will pass any particular point after 740.7  
seconds.

It then takes each passenger an average of 60 seconds to get from A to  
B, so that makes 800.7 seconds for the original question.

I would expect the real simulation answer to be slightly different  
because of the discontinuous (bursty) nature of arrivals.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 2:47pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/8 "2010-08-17T14:47:52Z")

</div>

> After 10,000 trials I got an average of 803 seconds, and after a second  
> 10,000 trials I got 807 seconds. But I don’t think we should post any  
> code until the OP has had a go.

Interesting. Sounds like I’ve got a bug to hunt down. Agreed, as far as  
posting code.

> (3) The vehicles depart on average every 20 seconds
> 
> So you should get an average of 0.675 passengers per second, which  
> implies 500 passengers will pass any particular point after 740.7  
> seconds.
> 
> It then takes each passenger an average of 60 seconds to get from A to  
> B, so that makes 800.7 seconds for the original question.

Nice analysis. I did a very similar set of calculations, and came up  
with  
roughly the same result.

> I would expect the real simulation answer to be slightly different  
> because of the discontinuous (bursty) nature of arrivals.

Can you expand on this? I’m curious what you mean, from a mathematical  
perspective.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/9 "2010-08-17T14:46:42Z")

</div>

2010/8/17 Andrew W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hmm, I may or may not disagree with you on what the output should be. I  
> can’t speak for his assignment,

It was just to get him the idea.

> Anyway, I did this, for fun, and consistently got an average of around 833  
> seconds on average for 100000 trials. Anyone else give this a shot? I’m  
> curious to see whether different implementations give different results. The  
> standard deviation seems to be pretty high.

I also did, twice on 100000 trials:  
First: In mean, it took 808.3 seconds to go there.  
Second: In mean, it took 809.3 seconds to go there.

It’s quite consistent with the little calculation Brian explained.  
I would expect the mean result to always be above the 800.7 seconds  
Brian found as there is very little chance that the sum of the  
passengers arriving in town be exactly 500 (it ranges from 500 to 539  
due to the buses)

Cheers,

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 2:56pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/10 "2010-08-17T14:56:39Z")

</div>

> It’s quite consistent with the little calculation Brian explained.  
> I would expect the mean result to always be above the 800.7 seconds  
> Brian found as there is very little chance that the sum of the  
> passengers arriving in town be exactly 500 (it ranges from 500 to 539  
> due to the buses)

Ah yes, I see now what Brian meant by “bursty”. Thanks for the results!

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/11 "2010-08-17T15:19:06Z")

</div>

A superhighway connects one large metropolitan area to another.  
A vehicle leaves the first city every 5-35sec. 20% of the vehicles have  
1 passenger, 30% have 2 passengers, 10% have 3 passengers and and 10%  
have 4 passengers. the remaining 30% of the vehicles are buses, which  
carry 40 people.  
It takes 50sec-1min &10sec for vehicles to travel between the two  
metropolitan  
areas. How long does it take for 500 people to arrive in the second  
city.

20% of vehicles (1passenger) -\> must transfer 100(20% of 500) people  
from A -\> B

30% of vehicles (2passenger) -\> must transfer 150 people from A -\> B

10% of vehicles (3passenger) -\> must transfer 50 people from A -\> B

10% of vehicles (4passenger) -\> must transfer 50 people from A -\> B

30% of vehicles(bus) (3passenger) -\> must transfer 150 people from A -\>  
B

5-35sec (rand?) -\> departure time for vehicles from A to B +  
50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -\> traveling time from A  
-\> B

and then

50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -\> traveling time from B  
-\> A  
5-35sec (rand?) -\> departure time for vehicles from B -\> A

i would like to if you used rand() in departure time/traveling time…

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/12 "2010-08-17T15:19:34Z")

</div>

A superhighway connects one large metropolitan area to another.  
A vehicle leaves the first city every 5-35sec. 20% of the vehicles have  
1 passenger, 30% have 2 passengers, 10% have 3 passengers and and 10%  
have 4 passengers. the remaining 30% of the vehicles are buses, which  
carry 40 people.  
It takes 50sec-1min &10sec for vehicles to travel between the two  
metropolitan  
areas. How long does it take for 500 people to arrive in the second  
city.

20% of vehicles (1passenger) -\> must transfer 100(20% of 500) people  
from A -\> B

30% of vehicles (2passenger) -\> must transfer 150 people from A -\> B

10% of vehicles (3passenger) -\> must transfer 50 people from A -\> B

10% of vehicles (4passenger) -\> must transfer 50 people from A -\> B

30% of vehicles(bus) (3passenger) -\> must transfer 150 people from A -\>  
B

5-35sec (rand?) -\> departure time for vehicles from A to B +  
50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -\> traveling time from A  
-\> B

and then

50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -\> traveling time from B  
-\> A  
5-35sec (rand?) -\> departure time for vehicles from B -\> A

i would like to know if you used rand() in departure time/traveling  
time…

Cheers

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/13 "2010-08-17T15:28:17Z")

</div>

> i would like to know if you used rand() in departure time/traveling  
> time…

I used rand() for both departure time and traveling time, assuming all  
the  
times were equally likely. You bring up an interesting point about going  
from B -\> A. In my simulation, I only considered vehicles going in one  
direction. Seems like the specification is unclear about whether they  
are  
leaving every 5-35 seconds from both cities or just one. Though if  
they’re  
going from both directions, there’s certainly no guarantee that you will  
ever move a total of 500 from one to the other.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/14 "2010-08-17T15:22:03Z")

</div>

sorry for the double post 🙂

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/15 "2010-08-17T15:29:32Z")

</div>

Bruce W. wrote:

> i would like to know if you used rand() in departure time/traveling  
> time…

Yes. rand() by itself gives a random number between 0.0 and 1.0 (well  
actually 0.99999999…). Hence, rand\*30.0 gives you a value between 0  
and 30.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:31pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/16 "2010-08-17T15:31:13Z")

</div>

Hello Bruce,

> 50sec - 1min & 10sec ( 70sec? im a ryt? rand?) -\> traveling time from B  
> -\> A  
> 5-35sec (rand?) -\> departure time for vehicles from B -\> A
> 
> i would like to know if you used rand() in departure time/traveling  
> time…

Yes, we did. But also when taking into account the number of  
passengers: in real life, on a certain amount of time, you don’t know  
if there will be a tad more buses and a tad less monodrivers than in  
mean. The only thing you do know is that if you count for a very long  
time, you end up with the given percentages.

Cheers,

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/17 "2010-08-17T15:35:55Z")

</div>

Ah. Yes, I didn’t mean to say that I used rand without any parameters. I  
used the fact that, for example, to get a random number from 0 to 5,  
inclusive, you can call rand(6).

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:36pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/18 "2010-08-17T15:36:07Z")

</div>

Bruce W. wrote:

> 20% of vehicles (1passenger) -\> must transfer 100(20% of 500) people  
> from A -\> B

No, and in any case that’s the wrong approach for a simulation.

In any particular run, it’s possible (although rather unlikely) that all  
the vehicles will depart will all be buses. Or that they will all be  
cars with one driver. In practice, the mix will vary between runs, and  
you don’t know in advance what it will be.

Treat each vehicle individually. Each one has its own departure time,  
its own arrival time, and its own number of occupants. If you do the  
simulation by hand, you’ll see what I mean.

To start with, you could use ruby as the random number generator, while  
you do the rest of the work by hand. Suppose the first vehicle leaves on  
the dot of 12:00:00 (or 0 seconds past midday). How many passengers does  
it contain? When does it arrive? When does the second vehicle leave? And  
so on.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:39pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/19 "2010-08-17T15:39:26Z")

</div>

Yes, but you take that into account by doing a large number of trials,  
right? You still consider all values equally likely in any individual  
case?

On Tue, Aug 17, 2010 at 9:29 AM, Jean-Julien F. \<

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [August 17, 2010, 3:34pm UTC](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412/20 "2010-08-17T15:34:09Z")

</div>

> You bring up an interesting point about going  
> from B -\> A. In my simulation, I only considered vehicles going in one  
> direction. Seems like the specification is unclear about whether they are  
> leaving every 5-35 seconds from both cities or just one.

Specifications are clear, at least to me:

“A vehicle leaves the first city every 5-35sec. […] How long does it  
take for 500 people to arrive in the second city.”

> Though if they’re  
> going from both directions, there’s certainly no guarantee that you will  
> ever move a total of 500 from one to the other.

Then the problem is turning into a (sort of) random walk: it’s getting  
far more complicated (but far more interesting ?)

Cheers,

[Next page](https://www.ruby-forum.com/t/can-i-do-this-in-ruby-a-simulation-process/193412.md?page=2)
