From: brabuhr@... Date: 2010-08-19T02:28:05+09:00 Subject: Re: can i do this in ruby? a simulation process On Wed, Aug 18, 2010 at 11:47 AM, Bruce Wayner wrote: > i already erase some codes: but the thing is how can i get the total > number of seconds every time vehicle arrive in the second city. > > Example: > Vehicle Number 1: > Departure Time: 3 seconds > Travel Time: 45seconds > # of people transfer: 1 > Total Time Consume: 48 --> how can get that one For any given vehicle: arrival_time = departure_time + travel_time If I read your problem statement correctly, what you are being asked for is the arrival time of the last vehicle (well, arrival time of the vehicle that has arrived-passenger number 500) > Here is my code which i delete some stuff to make it little: > print "Number of Vehicle: " #That will be maximum of 5 only > n = gets().to_i > > print "\n Vehicle number 1: " >  $delay1 = rand(4) >  $delay2 = rand(10) >  $depart = rand(36) >  $travelTime= rand(71) >  $ET = $depart >  $ET1= $travelTime >  $delay1 +=1 >  sleep($delay1) >  sleep($delay2) >  $passenger2= 1 >  $delay101 = rand(10) >  puts ("\nDeparture Time:  #$ET seconds"); >  puts ("Traveling Time:  #$ET1 seconds"); >  puts ("# of passenger Arrive in Second City: #{$passenger2}"); >  puts ("Total Traveling Time:     #{$delay1} seconds"); >  $i = 1+1; >  $num = n+1; Remove this whole vehicle 1 thing and just start the loop w/ i set to 1: >  while $i < $num  do > >  puts ("\n Vehicle number: #{$i}"); >  $i +=1; > >  $depart1 = rand(36) >  $travelTime1 = rand(71) >  $ET = $depart1 >  $ET1= $travelTime1 What are ET and ET1 intended to be? >  $passenger1=2 >     puts ("\nDeparture Time:  #$depart seconds"); >     puts ("Traveling Time:  #$travelTime1 seconds"); >    puts ("# of people Arrive in second city: #{$passenger1}"); >    puts ("Total Traveling Time:  #$totalTravel"); You never increment $totalTravel; it could be something like "$totalTravel += $travelTime1". Though that would not be the value your problem statement asked for. Consider the vehicle 1 leaves at time 0 and takes 30 seconds and vehicle 2 leaves at time 15 and takes 30 seconds. Adding the travel times together would give a total time of 60 seconds. However, both vehicles were on the road at the same time, so the actual time taken to deliver the passengers was 45 seconds. > end More importantly, in the loop you do not keep a running total of arrived passengers. Then, the loop should be keyed off of number of arrived passengers instead of number of vehicles. >  puts("# of people transfer:"); >  puts("Total Time:");