您的位置:首页 > 编程语言 > C语言/C++

C++11: random #2

2015-12-19 11:13 435 查看
#include <random>
#include <vector>
#include <string>
#include <iostream>

int main()
{
std::default_random_engine e;
std::normal_distribution<> n(4, 1.5);
std::vector<unsigned> vals(9);

for (std::size_t i = 0; i != 200; ++i)  {
unsigned v = std::lround(n(e));
if (v < vals.size()) {
++vals[v];
}
}

for (std::size_t j = 0; j < vals.size(); ++j) {
std::cout << j << ": " << std::string(vals[j], '*') << "\n";
}

std::default_random_engine e2;
std::uniform_real_distribution<double> u(0, 1);
for (std::size_t k = 0; k < 10; ++k) {
std::cout << u(e2) << " ";
}
std::cout << "\n";

std::default_random_engine e3;
std::bernoulli_distribution b;
std::string resp;
do {
bool first = b(e3);
std::cout << (first ? "We go first" : "You get to go first")
<< std::endl;
std::cout << "play again? Enter 'yes' or 'no'" << std::endl;
} while (std::cin >> resp && resp[0] == 'y');

return 0;
}


// from C++ Primer 5th Section 17.4.2(p.749)
// g++ xx.cpp -std=c++11
// gcc 4.9.2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: