2012-04-06 110 views
4

我需要生成X隨機雙數均勻分佈在兩個區間[a,b]之間,其中ab也是雙數。隨機數生成兩個區間之間的雙打[a,b]

那些X數字需要在類函數內部產生,比如myclass::doSomething(a,b)。問題在於,每當doSomething(a,b)函數被另一個類函數調用時,傳遞給doSomething(a,b)函數的時間間隔[a,b]就會改變,如doThat()

我想一個解決方案,讓我:
1.具有較高的作用域的engine,理想情況下,應接種每個應用程序只運行一次。
2. X在對doSomething()函數的每個單個調用中生成的隨機雙數應該是均勻分佈的。

我在下面的解決方案不允許更高的範圍engine和似乎生成的數字不一定是均勻分佈。

//file: utilities.h 
template <typename Generator> 
double randomDoubleEngine(Generator& engine, double low_bound, double high_bound) 
{ 
     if (low_bound > high_bound){ 
       std::swap(low_bound, high_bound); 
     } 

     return std::uniform_real_distribution<>(low_bound, high_bound)(engine); 
} 

//file: myclass.h 
     void myclass::doThat(param1, param2){ 

      for(int i=0; i < myclass.iter; i++){ 
       ... 
       ... 
       doSomething(a,b); 
       ... 
      } 

     } 

     void myclass::doSomething(double a, double b) 
     { 
       std::random_device rd; 
       static std::mt19937 engine(rd()); 
       ..... 
       double curThreshold = randomDoubleEngine(engine, a, b); 
       ... 
     } 
+7

您的解決方案是否正常工作?如果是這樣,你需要解決什麼問題?換句話說,SO讀者有什麼問題? – 2012-04-06 13:45:38

+1

@OliCharlesworth,我的解決方案並不完全工作。我更新了我原來的帖子。首先,「引擎」不是全球性的,其次,生成的數字似乎不是均勻分佈的。 – Tin 2012-04-06 13:49:57

+4

您使用什麼測試來確定分佈的均勻性,以及您的樣本量是多少?順便說一句,應該指出的是,兩個數字之間的差異是**不是均勻分佈的。 – MSalters 2012-04-06 14:04:22

回答

2

我想你想讓引擎成爲myclass的靜態成員。我不確定這會對你有什麼影響,除非你需要在其他方法中使用引擎。我在下面貼上了一個可能的解決方案。

請注意,與標準相比,gcc的外觀看起來不正確(請參閱代碼註釋中的鏈接),所以如果您使用它,可能會解釋爲什麼您對這些數字應用的任何測試(檢查均勻分佈)是失敗。據我所知,海灣合作委員會希望引擎返回數字在[0,1),而標準說,它應該是統一的整數在一定範圍內。

恐怕我只能用gcc 4.4來測試這個,因爲我運行的是較舊的Ubuntu版本,而ideone似乎不允許std :: random_device。

#include <random> 
#include <iostream> 

/* In GCC 4.4, uniform_real_distribution is called uniform_real; renamed in 4.5 
* 
* However, GCC's description here 
* 
* http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-api-4.6/a00731.html 
* 
* doesn't match expectation here 
* 
* http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution 
* 
* which seems to match 26.5.8.2.2 of N3337 
* 
*/ 
#if defined(__GNUC_MINOR__) && (__GNUC_MINOR__ <= 4) 
# define uniform_real_distribution uniform_real 
#endif 

template <typename Generator> 
double randomDoubleEngine(Generator& engine, double low_bound, double high_bound) 
{ 
    if (low_bound > high_bound){ 
    std::swap(low_bound, high_bound); 
    } 
    return std::uniform_real_distribution<double>(low_bound, high_bound)(engine); 
} 

class myclass 
{ 
    double curThreshold; 
    static std::mt19937 engine; 
    void doSomething(double a, double b) 
    { 
    curThreshold= randomDoubleEngine(engine, a, b); 
    } 
public: 
    myclass(): curThreshold(0) {} 

    void doThat(){ 
    doSomething(0,10); 
    std::cout << "threshold is " << curThreshold << std::endl; 
    } 
}; 

std::mt19937 myclass::engine=std::mt19937(std::random_device()()); 

int 
main() 
{ 
    myclass m; 
    m.doThat(); 
    m.doThat(); 
    return 0; 
} 
+0

看起來像OP的尋找。 – 2012-07-26 14:47:49

+0

這是不一樣的。 uniform_real_distribution在[a,b)範圍內生成,而不是[a,b] – 2013-06-06 11:00:49

相關問題