C++ piecewise_linear_distribution分段线性分布随机数用法详解
// Demonstrating the piecewise linear distribution #include <random> // For distributions and random number generator #include <vector> // For vector container #include <map> // For map container #include <utility> // For pair type #include <algorithm> // For copy(), count(), remove() #include <iostream> // For standard streams #include <iterator> // For stream iterators #include <iomanip> // For stream manipulators #include <string> // For string class using std::string; int main() { std::vector<double> v {10, 30, 40, 55, 60}; // Sample values std::vector<double> w {6, 12, 9, 6, 0}; // Weights for the samples std::piecewise_linear_distribution<> d {std::begin(v), std::end(v), std::begin(w)}; // Output the interval boundaries and the interval probabilities auto values = d.intervals(); std::cout << "Sample values: "; std::copy(std::begin(values), std::end(values), std::ostream_iterator<double>{std::cout, " "}); std::cout << " probability densities: "; auto probs = d.densities(); std::copy(std::begin(probs), std::end(probs), std::ostream_iterator<double>{std::cout, " "}); std::cout << '\n' << std::endl; std::random_device rd; std::default_random_engine rng {rd()}; std::map<int, size_t> results; //Stores and counts random values as integers // Generate a lot of random values... for(size_t i {}; i < 20000; ++i) ++results[static_cast<int>(std::round(d(rng)))]; // Plot the integer values auto max_count = std::max_element(std::begin(results), std::end(results),[](const std::pair<int, size_t>& pr1, const std::pair<int, size_t>& pr2) { return pr1.second < pr2.second; })->second; std::for_each(std::begin(results), std::end(results),[max_count](const std::pair<int, size_t>& pr) { if(!(pr.first % 10)) // Display value if multiple of 10 std::cout << std::setw(3) << pr.first << "-|"; else std::cout << " |"; std::cout << std::string(pr.second * 80 / max_count, '*')<< '\n'; }); }
Sample values: 10 30 40 55 60 probability densities: 0.0145455 0.0290909 0.0218182 0.0145455 0
10-|**********************
|*******************************************
|*******************************************
|***************************************
|*********************************************
|*****************************************************
|**************************************************
|*********************************************************
|********************************************************
|**************************************************************
20-|*********************************************************
|*************************************************************
|************************************************************
|******************************************************************
|******************************************************************
|**********************************************************************
|***************************************************************************
|*****************************************************************************
|******************************************************************************
|*******************************************************************************
30-|*****************************************************************************
|********************************************************************************
|****************************************************************************
|********************************************************************
|************************************************************************
|******************************************************************************
|************************************************************************
|********************************************************************
|************************************************************
|***********************************************************
40-|**************************************************************
|************************************************************
|*****************************************************
|*******************************************************
|*********************************************************
|********************************************************
|***********************************************
|***********************************************
|*************************************************
|**********************************************
50-|******************************************
|***********************************************
|********************************************
|*******************************************
|***************************************
|**************************************
|**********************************
|**********************
|**************
|******
60-|*
所有教程
- socket
- Python基础教程
- C#教程
- MySQL函数
- MySQL
- C语言入门
- C语言专题
- C语言编译器
- C语言编程实例
- GCC编译器
- 数据结构
- C语言项目案例
- C++教程
- OpenCV
- Qt教程
- Unity 3D教程
- UE4
- STL
- Redis
- Android教程
- JavaScript
- PHP
- Mybatis
- Spring Cloud
- Maven
- vi命令
- Spring Boot
- Spring MVC
- Hibernate
- Linux
- Linux命令
- Shell脚本
- Java教程
- 设计模式
- Spring
- Servlet
- Struts2
- Java Swing
- JSP教程
- CSS教程
- TensorFlow
- 区块链
- Go语言教程
- Docker
- 编程笔记
- 资源下载
- 关于我们
- 汇编语言
- 大数据
- 云计算
- VIP视频