开发者

event driven simulation with objects

i am wri开发者_开发知识库ting 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


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();
   }
 ........
 }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜