Created
February 6, 2013 13:45
-
-
Save belltailjp/4722578 to your computer and use it in GitHub Desktop.
一様乱数だけあるときのおみくじプログラム
This file contains 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 <iostream> | |
#include <random> | |
int main() | |
{ | |
std::vector<int> prob = {1, 9, 25, 25, 30, 9, 1}; | |
const std::string str[] = {"大吉", "中吉", "小吉", "吉末", "吉", "凶", "大凶"}; | |
std::mt19937 rng; | |
//出現確率を累積に変換 | |
int acc = 0; | |
for(auto p = prob.begin(); p != prob.end(); ++p) | |
acc = *p = acc + *p; | |
//おみくじを引きまくる | |
while(1) | |
{ | |
const int t = rng() % 100; | |
for(auto p = prob.begin(); p != prob.end(); ++p) | |
{ | |
if(t < *p) | |
{ | |
std::cout << str[p - prob.begin()] << std::endl; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment