/*轮盘赌*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXCHOICENUMBER 101
int main(){
int choiceNumber;
double probabilities[MAXCHOICENUMBER];
double threshold;
int theChoice;
int counter;
srand((unsigned)time(NULL));
printf("Input the total number of choices:");
scanf_s("%d", &choiceNumber);
printf("Input the probability of each choice:");
for(counter = 0; counter < choiceNumber; counter++) {
scanf_s("%lf", &probabilities[counter]);
}
for(counter = 1; counter < choiceNumber; counter++) {
probabilities[counter] = probabilities[counter] + probabilities[counter - 1];
}
threshold = (double)rand() / (double)RAND_MAX;
for(counter = 0; counter < choiceNumber; counter++) {
if(probabilities[counter] >= threshold) {
theChoice = counter;
break;
}
}
printf("The choice is: %d\n", theChoice);
system("pause");
return 0;
}