Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Created February 6, 2013 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/4722578 to your computer and use it in GitHub Desktop.
Save belltailjp/4722578 to your computer and use it in GitHub Desktop.
一様乱数だけあるときのおみくじプログラム
#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