创建具有平均值和标准偏差的高斯随机生成器 [英] Creating a Gaussian Random Generator with a mean and standard deviation

查看:777
本文介绍了创建具有平均值和标准偏差的高斯随机生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个一维数组并使用随机数生成器(生成平均值为70和标准差为10的随机数的高斯生成器),使用至少100个0到100之间的数字填充数组

$

解决方案

C ++ 11 中,这是相对简单的使用随机头 std :: normal_distribution 实例 ):

  include< iostream> 
#include< iomanip>
#include< string>
#include< map>
#include< random>

int main()
{
std :: random_device rd;

std :: mt19937 e2(rd());

std :: normal_distribution<> dist(70,10);

std :: map< int,int> hist;
for(int n = 0; n <100000; ++ n){
++ hist [std :: round(dist(e2))]
}

for(auto p:hist){
std :: cout< std :: fixed<< std :: setprecision(1)<< std :: setw(2)
<< p.first<< ''<< std :: string(p.second / 200,'*')<< '\\\
';
}
}

如果 C ++ 11 不是 boost 选项,还提供了一个库( 实例 ):

  #include< iostream> 
#include< iomanip>
#include< string>
#include< map>
#include< random>
#include< boost / random.hpp>
#include< boost / random / normal_distribution.hpp>

int main()
{

boost :: mt19937 * rng = new boost :: mt19937();
rng-> seed(time(NULL));

boost :: normal_distribution<>分布(70,10);
boost :: variate_generator< boost :: mt19937,boost :: normal_distribution<> > dist(* rng,distribution);

std :: map< int,int> hist;
for(int n = 0; n< 100000; ++ n){
++ hist [std :: round(dist())];
}

for(auto p:hist){
std :: cout< std :: fixed<< std :: setprecision(1)<< std :: setw(2)
<< p.first<< ''<< std :: string(p.second / 200,'*')<< '\\\
';
}
}

如果由于某种原因,那么您可以自行进行 Box-Muller变换,链接中提供的代码看起来合理。 p>

I am trying to create a one dimensional array and use a random number generator(Gaussian generator that generates a random number with means of 70 and a standard deviation of 10) to populate the array with at least 100 numbers between 0 and 100 inclusive.

How would i go about doing this in C++?

解决方案

In C++11 this is relatively straight forward using the random header and std::normal_distribution (live example):

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>

int main()
{
    std::random_device rd;

    std::mt19937 e2(rd());

    std::normal_distribution<> dist(70, 10);

    std::map<int, int> hist;
    for (int n = 0; n < 100000; ++n) {
        ++hist[std::round(dist(e2))];
    }

    for (auto p : hist) {
        std::cout << std::fixed << std::setprecision(1) << std::setw(2)
                  << p.first << ' ' << std::string(p.second/200, '*') << '\n';
    }
}

If C++11 is not an option than boost also provides a library(live example):

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>

int main()
{

  boost::mt19937 *rng = new boost::mt19937();
  rng->seed(time(NULL));

  boost::normal_distribution<> distribution(70, 10);
  boost::variate_generator< boost::mt19937, boost::normal_distribution<> > dist(*rng, distribution);

  std::map<int, int> hist;
  for (int n = 0; n < 100000; ++n) {
    ++hist[std::round(dist())];
  }

  for (auto p : hist) {
    std::cout << std::fixed << std::setprecision(1) << std::setw(2)
              << p.first << ' ' << std::string(p.second/200, '*') << '\n';
  }
}

and if for some reason neither of these options is possible then you can roll your own Box-Muller transform, the code provided in the link looks reasonable.

这篇关于创建具有平均值和标准偏差的高斯随机生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆