Created
May 15, 2014 13:17
-
-
Save KOBA789/01bf810cb3435f7a9bfa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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