views:

69

answers:

2

hi everyone,

i am writing an event driven simulation program. i have 3 subclasses that inherit 1 base class. i need to generate those three randomly and each subclass will go through different event path (sorry its a bit hard to describe what i meant), ill give an example:

let say we have a car park simulation at a mall, we have the base class Vehicle, and subclasses Car, Motorbike, TruckContainer. the Car and Motorbike is just going to park for a period of time(random) and leave while TruckContainer need to park only for unloading and loading the container and leave, the number of container will decide how long the truck will park.

how can i create those 3 objects randomly, let say 5-10 car will enter the car park in 1minute, 1-3 motorbike in 10minute, and only 1-2 truck container in a day?

thank you

A: 

This might lead u there

VehicleControl::VehicleControl() {
mapOfFreq["Car"] = 6;     // based on 10 per 60 sec
mapOfFreq["Bike"] = 200;
.....
}

vehicle* VehicleControl::getVehicle() {
time_t t = time();
  if (t - mapOfCreatedTime["Car"] > mapOfFreq["Car"]) {
    mapOfCreatedTime["Car"] = t;
    return new Car();
   }
 ........
 }
aeh
im not sure what is mapOfFreq and mapOfCreatedTime, are they represent int? or time? or??
chandra wib
@chandra: mapOfFreq and mapOfCreatedTime can be map<string, unsigned>
aeh
A: 

So as far as I understood You want a random number generator within the limits of (for example your car class) 5-10, 1-2 (truck) and 1-3 (bike)...

You may do this by using the pseudo-random-number generator rand()...

for Your car:

rand() % 10 + 5; //from 5 to 10

but dont forget to initialize your rand via srand()!...

Of course You need to control when in the time slice (for example the 10 minutes) the bikes will arrive...

Hope this helps

Incubbus