Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Math 从有误差范围的分布中取样_Math_Random_Statistics_Distribution - Fatal编程技术网

Math 从有误差范围的分布中取样

Math 从有误差范围的分布中取样,math,random,statistics,distribution,Math,Random,Statistics,Distribution,以下代码对加权随机分布进行采样,作为模拟的一部分,表示100k个人可能采取的选项(例如:投票等) 有两种可能的选择,权重分别为30%和70% #include <iostream> #include <random> int main() { int option0 = 30; //30% int option1 = 70; //30% std::vector<int> option({0,0}); std::random_dev

以下代码对加权随机分布进行采样,作为模拟的一部分,表示100k个人可能采取的选项(例如:投票等)

有两种可能的选择,权重分别为30%和70%

#include <iostream>
#include <random>

int main()
{
   int option0 = 30; //30%
   int option1 = 70; //30%

   std::vector<int> option({0,0});

   std::random_device rd;
   std::mt19937 gen(rd());
   std::discrete_distribution<> d({option0,option1});

   for (int n=0; n < 100000; ++n)
   {
      ++option[d(gen)];
   }

   std::cout << "Option 0: " << option[0] << std::endl;
   std::cout << "Option 1: " << option[1] << std::endl;

   return 0;
}
#包括
#包括
int main()
{
int option0=30;//30%
int option1=70;//30%
std::vector选项({0,0});
std::随机_装置rd;
标准:mt19937 gen(rd());
离散分布d({option0,option1});
对于(int n=0;n<100000;++n)
{
++选择权[d(gen)];
}

std::cout因为您确切地知道误差幅度,所以可以使用 随机生成的双精度(介于-5.0和5.0之间),然后运行建议的模拟

此功能将模拟所需的误差范围:

void
simulating_margin_of_error(std::mt19937 gen, double marginOfError,
                           int* option0, int* option1)
{
    std::uniform_real_distribution<double>
        distribution(-marginOfError,marginOfError);
    double number = distribution(gen);

    *option0 += (int) number;
    *option1 += - (int) number;
}
void
模拟误差的裕度(std::mt19937 gen,双裕度),
整数*选项0,整数*选项1)
{
标准:均匀实分布
分布(-marginOfError,marginOfError);
双倍数字=分布(gen);
*选项0+=(整数)编号;
*选项1+=-(整数)编号;
}
您可以将其包括在此处:

[...]
std::mt19937 gen(rd());
simulating_margin_of_error(gen, 5.0, &option0, &option1);
std::discrete_distribution<> d({option0, option1});
[...]
[…]
标准:mt19937 gen(rd());
模拟误差的裕度(gen、5.0、选项0和选项1);
离散分布d({option0,option1});
[...]