C 随机函数,以概率选择可变IP数';s

C 随机函数,以概率选择可变IP数';s,c,C,我有3个IP,每个IP都有一个权重,我想使用随机函数根据其权重返回IP,,,,例如,如果我们有3个IP:X权重为6,Y权重为4,Z权重为2,我想在50%的情况下返回X,在33%的情况下返回Y,在17%的情况下返回Z,这取决于C中的随机函数 此代码适用于3个IP的情况: double r = rand() / (double)RAND_MAX; double denom = 6 + 4 + 2; if (r < 6 / denom) { // choose X } else if (r &l

我有3个IP,每个IP都有一个权重,我想使用随机函数根据其权重返回IP,,,,例如,如果我们有3个IP:X权重为6,Y权重为4,Z权重为2,我想在50%的情况下返回X,在33%的情况下返回Y,在17%的情况下返回Z,这取决于C中的随机函数

此代码适用于3个IP的情况:

double r = rand() / (double)RAND_MAX;
double denom = 6 + 4 + 2;
if (r < 6 / denom) {
// choose X
} else if (r < (6 + 4) / denom) {
// choose Y 
} else {
// choose Z
}
double r=rand()/(double)rand_MAX;
双精度=6+4+2;
如果(r<6/denom){
//选择X
}否则如果(r<(6+4)/denom){
//选择Y
}否则{
//选择Z
}

如果我有n个ip,我如何修改代码来处理n个ip而不是特定数量的ip呢?

使用ip的累积权重构建一个数组

像这样的

// C99 code
int pick_ip(int weights[], int nweights)
{
    // note you can split this step out if you like (a good plan)
    int cum_weights[nweights];
    int tot_weight = 0;
    for(int i=0; i < nweights; i++)
    {
        tot_weight += weights[i];
        cum_weights[i] = tot_weight;
    }

    int t = (int)(tot_weight * rand() / (double)RAND_MAX);

    if(cum_weights[0] > t) { return 0; }

    // binary search for point that we picked
    int v = -1;
    int l = 0, u = nweights -1;
    int m = u/2;
    do { // binary search
        if(cum_weights[m] > t) u = m;
        else l = m;
        m = (u + l)/2;
        if(cum_weights[l+1] >  t) {v=l+1; break;}
        if(cum_weights[u-1] <= t) {v=u;   break;}
    } while(1);
}
//C99代码
整数选取ip(整数权重[],整数权重)
{
//注意:如果你愿意,你可以把这一步分开(一个好计划)
单位重量[单位重量];
int tot_权重=0;
对于(int i=0;it{返回0;}
//二进制搜索我们选择的点
int v=-1;
int l=0,u=nweights-1;
int m=u/2;
执行{//二进制搜索
如果(cum_权重[m]>t)u=m;
否则l=m;
m=(u+l)/2;
如果(cum_权重[l+1]>t{v=l+1;break;}

如果(cum_-weights[u-1]使用ip的累积权重构建一个数组

像这样的

// C99 code
int pick_ip(int weights[], int nweights)
{
    // note you can split this step out if you like (a good plan)
    int cum_weights[nweights];
    int tot_weight = 0;
    for(int i=0; i < nweights; i++)
    {
        tot_weight += weights[i];
        cum_weights[i] = tot_weight;
    }

    int t = (int)(tot_weight * rand() / (double)RAND_MAX);

    if(cum_weights[0] > t) { return 0; }

    // binary search for point that we picked
    int v = -1;
    int l = 0, u = nweights -1;
    int m = u/2;
    do { // binary search
        if(cum_weights[m] > t) u = m;
        else l = m;
        m = (u + l)/2;
        if(cum_weights[l+1] >  t) {v=l+1; break;}
        if(cum_weights[u-1] <= t) {v=u;   break;}
    } while(1);
}
//C99代码
整数选取ip(整数权重[],整数权重)
{
//注意:如果你愿意,你可以把这一步分开(一个好计划)
单位重量[单位重量];
int tot_权重=0;
对于(int i=0;it{返回0;}
//二进制搜索我们选择的点
int v=-1;
int l=0,u=nweights-1;
int m=u/2;
执行{//二进制搜索
如果(cum_权重[m]>t)u=m;
否则l=m;
m=(u+l)/2;
如果(cum_权重[l+1]>t{v=l+1;break;}

如果(cum_)权重[u-1],这里是一个如何执行此操作的示例

从这个帖子:

int sum_of_weight = 0;
for(int i=0; i<num_choices; i++) {
   sum_of_weight += choice_weight[i];
}
int rnd = random(sum_of_weight);
for(int i=0; i<num_choices; i++) {
  if(rnd < choice_weight[i])
    return i;
  rnd -= choice_weight[i];
}
assert(!"should never get here");
权重的整数和=0;
对于(int i=0;i,这里是一个如何执行此操作的示例

从这个帖子:

int sum_of_weight = 0;
for(int i=0; i<num_choices; i++) {
   sum_of_weight += choice_weight[i];
}
int rnd = random(sum_of_weight);
for(int i=0; i<num_choices; i++) {
  if(rnd < choice_weight[i])
    return i;
  rnd -= choice_weight[i];
}
assert(!"should never get here");
权重的整数和=0; 对于(int i=0;i