Skip to content

Instantly share code, notes, and snippets.

@KOBA789
Created May 15, 2014 13:17
Show Gist options
  • Save KOBA789/01bf810cb3435f7a9bfa to your computer and use it in GitHub Desktop.
Save KOBA789/01bf810cb3435f7a9bfa to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int pick(int *rate, int types) {
int i = 0, max = 0, sum = 0, rnd;
for (i = 0; i < types; ++i) {
max += rate[i];
}
rnd = rand() % max;
for (i = 0; i < types; ++i) {
sum += rate[i];
if (sum > rnd) { return i; }
}
return -1;
}
int main() {
char* types[] = {
"大吉",
"中吉",
"小吉",
"吉",
"末吉",
"凶"
};
int rate[] = {
5, /* 大吉 */
3, /* 中吉 */
1, /* 小吉 */
1, /* 吉 */
1, /* 末吉 */
7 /* 凶 */
};
srand((unsigned) time(NULL));
printf("%s\n", types[pick(rate, 6)]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment