[英]Generate a complete Gaussian/normal distribution in C++ in the distributed order
[英]How to generate a well distributed normal distribution with a Sobol sequence in c++?
我有以下代碼:
#include <iostream>
#include <vector>
#include <random>
// #include "halton.cpp"
#include "sobol.cpp"
int main()
{
int n=5000;
double* thisV;
thisV = i8_sobol_generate(1,n,0);
std::mt19937 generator;
std::normal_distribution<double> distribution(0,1.0);
for (int i=0; i<n; i++) {
generator.seed(thisV[i]);
std::cout << distribution(generator) << std::endl;
}
return 0;
}
我使用本 網站 (Sobol)上的以下代碼生成了一個低差異序列。 我用它來播種發電機。 但是輸出有點奇怪。 有人能幫我嗎?
輸出:
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
1.12279
0.302805
...
如果沒有看到 sobol.cpp 文件的內容,我無法告訴您為什么會得到這些確切的數字,但是您每次循環時都會重新設置生成器的種子。
如果 std::mt19937 生成器;
std::normal_distribution<double> distribution(0,1.0);
for (int i=0; i<n; i++) {
generator.seed(thisV[i]); //<------------
std::cout << distribution(generator) << std::endl;
}
如果種子在兩個值之間交替,您將獲得您所看到的行為。
順便說一句 - 通常#include
一個頭文件(而不是一個 cpp 文件),同時在你的 rmake 文件/項目中包含 cpp 文件。
此外,如果您查看調用代碼的方式,
thisV = i8_sobol_generate(1,n,0);
我們還沒有澄清參數的含義。
如果您按照您擁有的鏈接進行操作,則會對源代碼進行一些測試(哇!)。 一個,被調用,測試 sorbol08,並循環如下:
for ( dim_num = 2; dim_num <= DIM_MAX; dim_num++ ) { //^--------- 種子 = 0;
// <snip>
for ( i = 0; i <= 110; i++ )
{
seed_in = seed;
i8_sobol ( dim_num, &seed, r );
// ....
你調用的函數,
double *i8_sobol_generate ( int m, int n, int skip )
正在發送 1 作為第一個參數。 這將使用此值調用測試函數。
因此,我懷疑您應該嘗試更高的維度 - 它確實循環非常快。
請包括 .hpp 文件而不是 cpp 文件。 查看測試以獲取有關使用的線索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.