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
相关文章推荐
- C++类模板的定义
- 实战c++中的vector系列--知道emplace_back为何优于push_back吗?
- 实战c++中的vector系列--知道emplace_back为何优于push_back吗?
- C语言数组和指针
- C++静态类
- c++继承
- C++中将输出指定到文件的方法:
- C++11: random #1
- C语言逗号运算符和逗号表达式
- 二叉搜索树(c++)
- vs2013安装caffe过程中,项目属性->项类型中缺少CUDA C/C++
- 一起talk C栗子吧(第七十七回:C语言实例--DIY ls命令续)
- 【C++ STL应用与实现】目录
- c++之路起航——指针
- C++ Primer(十五) 函数
- 实战c++中的vector系列--creating vector of local structure、vector of structs initialization
- 实战c++中的vector系列--creating vector of local structure、vector of structs initialization
- fastcgi协议分析与实例
- 《C语言及程序设计初步》第七讲实践项目
- Chapter 8-02