c++高斯分布函数normal_distribution介绍以及不随机问题解决办法
官方介绍:
示例代码:
// normal_distribution
#include <iostream>
#include <string>
#include <random>int main()
{const int nrolls=10000; // number of experimentsconst int nstars=100; // maximum number of stars to distributestd::default_random_engine generator;std::normal_distribution<double> distribution(5.0,2.0);int p[10]={};for (int i=0; i<nrolls; ++i) {double number = distribution(generator);if ((number>=0.0)&&(number<10.0)) ++p[int(number)];}std::cout << "normal_distribution (5.0,2.0):" << std::endl;for (int i=0; i<10; ++i) {std::cout << i << "-" << (i+1) << ": ";std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;}return 0;
}
输出:
normal_distribution (5.0,2.0): 0-1: * 1-2: **** 2-3: ********* 3-4: *************** 4-5: ****************** 5-6: ******************* 6-7: *************** 7-8: ******** 8-9: **** 9-10: *
我在自己写程序时遇到一个问题,就是函数生成的数据不随机,只有两组数据,示例代码:
#include <iostream>
#include <random>
int main() {const int x=50;const int y=22;int p[200];//std::normal_distribution<double> distribution(x, 2.0);std::normal_distribution<double>* yawGauss = new std::normal_distribution<double>(0,2);for (int i = 0; i <250 ; ++i) {std::default_random_engine generator;double number = y+(*yawGauss)(generator);if ((number >= 0.0) && (number<100.0))++p[i]=number;}for (int j = 0; j <250 ; ++j) {std::cout<<"Distribution "<<j<<":\t"<<p[j]<<std::endl;}std::cout << "Hello, World!" << std::endl;return 0;
}
输出:
/home/allen/CLionProjects/gaussrandom/cmake-build-debug/gaussrandom
Distribution 0: 21
Distribution 1: 19
Distribution 2: 21
Distribution 3: 19
Distribution 4: 21
Distribution 5: 19
Distribution 6: 21
Distribution 7: 19
Distribution 8: 21
Distribution 9: 19
Distribution 10: 21
Distribution 11: 19
Distribution 12: 21
Distribution 13: 19
......
原因是因为
std::default_random_engine generator;
当genegator放在循环里面时,就会出现这种情况,只需要将其从循环中拿出,这个generator只需要一次实例化即可。
c++高斯分布函数normal_distribution介绍以及不随机问题解决办法相关推荐
- PBR:双向反射分布函数(BRDF)介绍与Cook-Torrance模型的实现
PBR:双向反射分布函数(BRDF)介绍与Cook-Torrance模型的实现 BRDF简介 再介绍BRDF之前我们要引入渲染方程这个东西: 其中L表示辐射率,其公式为: 它表示了一个拥有辐射强度Φ的 ...
- 高斯噪声和高斯白噪声相关介绍
高斯噪声是一种随机噪声,在任选瞬时中任取n个,其值按n个变数的高斯概率定律分布. 注: 1,高斯噪声完全由其时变平均值和两瞬时的协方差函数来确定,若噪声为平稳的,则平均值与时间无关,而协方差函数则变成 ...
- MATLAB实现多峰高斯函数拟合/多峰高斯分布函数拟合(MATLAB全代码)
基于MATLAB实现多峰高斯函数拟合/多峰高斯分布函数拟合,提供三类多峰函数拟合方法,分别为双峰函数.三峰函数和自定义高斯函数. 1 原理 2 MATLAB代码实现 MATLAB函数如下: funct ...
- 高斯分布函数实现及绘图
挑战:参考高斯分布公式,使用 Python 实现高斯分布函数. 高斯分布公式 代码: """实现高斯分布函数 """ import nump ...
- 【转】两个高斯分布函数乘积的理论推导
[转]两个高斯分布函数乘积的理论推导 ---------------- 版权声明:本文为CSDN博主「chaosir」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明. ...
- easyconnect xp登录_easyconnect怎么用?easyconnect电脑版使用方法介绍(含无法连接解决办法)...
easyconnect是什么?easyconnect电脑版也就是深信服EasyConnect_install电脑客户端安装程序,安装Easyconnect之后,就能访问自己公司的内网系统及相关应用了, ...
- 华为云PB级数据库GaussDB(for Redis)介绍第四期:高斯 Geo的介绍与应用
摘要:高斯Redis的大规模地理位置信息存储的解决方案. 1.背景 LBS(Location Based Service,基于位置的服务)有非常广泛的应用场景,最常见的应用就是POI(Point of ...
- matlab计算高斯分布函数,MATLAB如何使用normcdf函数计算正态分布的累积概率
MATLAB如何使用normcdf函数计算正态分布的累积概率 [语法说明] P=normcdf(X,mu,sigma):计算X中的元素在均值 mu.标准差sigma确定的正态分布下的累积概率值.P是与 ...
- [MSSQL]SQL Server中的RAND函数的介绍和区间随机数值函数的实现
转自:http://www.cnblogs.com/dzy863/p/5063558.html 工作中会遇到SQL Server模拟数据生成以及数值列值(如整型.日期和时间数据类型)随机填充等等任务, ...
最新文章
- 企业级区块链现状研究报告:小企业的投资总额是大企业的28倍
- 排列组合 概率 贝叶斯理解
- GoWeb开发_Iris框架讲解(三):路由功能处理方式
- 3.8 神经网络解决逻辑运算问题-机器学习笔记-斯坦福吴恩达教授
- Flask学习 视频网站搭建(Python-flask视频网站搭建)
- 安卓APP_ 控件(3)—— EditText
- Flutter基础—第一个Flutter实例
- Asp.net2.0动态加载用户控件的三种方法
- python定义变量类型_一、python基础之数据类型与变量
- python绘制曲线y=2x+5_使用python动态生成波形曲线的实现
- Java并发之Condition接口
- Strip iPhone6
- As Giants Step In, Asustek Defends A Tiny PC
- 用Python做一个美丽的每日计划
- 信息提取 Information Extraction
- No matter how hard it is or no matter how bad it gets, I am going to make it!
- Tracking相关的文章
- Unity Recorder屏幕录制问题
- 3个月学习成功上岗软件测试,我一个文科女也能吃IT饭了
- 用Java 开发 WebService Axis简单实例