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-|*
所有教程
- C语言入门
- C语言编译器
- C语言项目案例
- 数据结构
- C++
- STL
- C++11
- socket
- GCC
- GDB
- Makefile
- OpenCV
- Qt教程
- Unity 3D
- UE4
- 游戏引擎
- Python
- Python并发编程
- TensorFlow
- Django
- NumPy
- Linux
- Shell
- Java教程
- 设计模式
- Java Swing
- Servlet
- JSP教程
- Struts2
- Maven
- Spring
- Spring MVC
- Spring Boot
- Spring Cloud
- Hibernate
- Mybatis
- MySQL教程
- MySQL函数
- NoSQL
- Redis
- MongoDB
- HBase
- Go语言
- C#
- MATLAB
- JavaScript
- Bootstrap
- HTML
- CSS教程
- PHP
- 汇编语言
- TCP/IP
- vi命令
- Android教程
- 区块链
- Docker
- 大数据
- 云计算