mardi 28 juillet 2020

Random objects assigned true or false in C++

I have to assign a certain amount of objects (based on percentage) that have been randomly assigned a true value for a paid subscription attribute, which then get sorted by priority and arrival time in a priority queue. Currently what I have would assign only the first arriving callers a subscribed (true) status and can't seem to think of an easy way to randomize which callers are paid and which are not since each call object is generated sequentially in a for loop.

For example: If there is an average of 25 callers per hr, 25% of them are subscribed and the simulation is for 24 hrs, I would generate 600 callers, 150 subscribed and 450 unsubscribed and randomly assign the 150 subscribed = true

I think I would need to change my for loop range limit to n, then randomly generate a true or false value for bool subscribed within the for loop, but also keep track of how many are iterations are true and false for bool subscribed, which I tried to implement below and still get a random amount of true/false.

Main

    CallCenter dRus; 
    Rep r1;
    bool subscribed = false;
    int percentSubscribed = 0;
    int numSubscribed;
    int numNotSubscribed;
    int countSubbed;
    int countNotSubbed;
    int n;


    n = 24;
    numSubscribed = (percentSubscribed / 100) * n;
    numNotSubscribed = n - numSubscribed;

    for (int i = 0; i <n; i++) //start sim by creating subscribed and unsubscribed callers 
    {
        subscribed = rand() % 2;
        if (subscribed == true) { countSubbed++; }
        else { countNotSubbed++; };
        
        if ((countSubbed > numSubscribed) )
        {
            subscribed = false;
            dRus.generateCalls(subscribed);
        }
        else {dRus.generateCalls(subscribed);};

        
    }

    subscribed = false;
    for (int i = 0; i < numNotSubscribed; i++) //create not subscribed callers
    {
        dRus.generateCalls(subscribed);
    }

Call Center

void CallCenter::generateCalls(bool subbed) //create a call object
{   
    Caller cust(subbed); 
    cust.generateInterArrivalTime();                //generate an inter-arrival time for the the call
    cust.setArrivalTime(cust.getInterArrivalTime()+ clock);    //set the arrival time 
    clock += cust.getInterArrivalTime();                      // update the clock variable 
    callQ.push(cust); //put the call in the queue
}



Aucun commentaire:

Enregistrer un commentaire