场景:有N个合作方,每一个合作方都有一定的权重,按权重随机选择一个合作方


typedef struct
{
string k;//partner_id
string v;//value
string m;//0:number 1:ratio
}Bookpartner_count_listInfo;


string GetRandNumRatio( vector<Bookpartner_count_listInfo> arpartner_count_list)
{
int weight = 0;
int user = 0;
Bookpartner_count_listInfo bp_cunt_list_temp[128];
for( vector<Bookpartner_count_listInfo>::iterator it = arpartner_count_list.begin(); it < arpartner_count_list.end(); it++)
{
weight += atoi(it->v.c_str());


for( int i = 0; i < atoi( it->v.c_str()); i++ )
{
*(bp_cunt_list_temp + weight - i) = *it;
}
}


struct timeval tv;

gettimeofday(&tv, NULL);
srand(tv.tv_usec);


user = rand() % weight;
Bookpartner_count_listInfo bk_pcl_temp = bp_cunt_list_temp[user];


return bk_pcl_temp.k;
}