我必须分配一定数量的对象(基于百分比),这些对象已经随机分配了付费订阅属性的真实值,然后按优先级和到达时间在优先级队列中进行排序。目前,我只为第一个到达的 call 者分配了已订阅(真实)状态,并且似乎没有想到一种简单的方法来随机化哪些 call 者已付款,哪些没有,因为每个 call 对象都是在for循环中顺序生成的。
例如:
如果平均每小时有25个 call 者,其中25%的用户已订阅,并且模拟持续24小时,则我将生成600个 call 者,150个订阅者和450个未订阅者,并随机分配150个subscribed = true
我想我需要将for循环范围限制更改为n
,然后在for循环内为bool subscribed
随机生成一个true或false值,还要跟踪bool subscribed
的迭代是true还是false,我尝试这样做实施以下内容,仍会获得随机的true / false。
主要
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 中心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
}
最佳答案
看来您的问题(从C个调用者中选择S个订阅者)可以解决:
shuffle
方法)。使用rand()
(尤其是rand() % 2
)并不是最好的方法,尤其是在C++中。请参阅以下问题:Why is the use of rand() considered bad?,Generating a random bit - lack of randomness in C rand()。 true
值分配给这些位置的调用方。 false
分配给其余调用者。