#include"iostream"
#include"stdlib.h"
#include"math.h"
#include"time.h"
using namespace std;
#define Size 10
typedef struct gIndividualMsg
{
int index;
double weight;
double value;
}IndividualMsg;
IndividualMsg Individual[10];
int gambleChoose()
{
int i,j;
int choose;
double l;
srand((unsigned int)time(NULL));
double SumValue=45;
double seed=(double(rand()%10000))/10000; //此处规定了重量,价值只能精确到小数点后两位
cout<<seed<<endl; //xxxx
for(choose=0;choose<Size;choose++)
{
l=0;
for(j=0;j<=choose;j++)
l+=Individual[j].value/SumValue;
if(l>=seed)
{
return choose;
break;
}
}
}
int main()
{
srand(time(0));
Individual[0].value=9;
Individual[1].value=8;
Individual[2].value=7;
Individual[3].value=6;
Individual[4].value=5;
Individual[5].value=4;
Individual[6].value=3;
Individual[7].value=2;
Individual[8].value=1;
Individual[9].value=0;
int a;
for(int i=0;i<100;i++)
{
a=gambleChoose();
cout<<a;
cout<<endl;
}
return 0;
}
慕森王
相关分类