2012-04-18 33 views
1

我想挑選一個單位球體上的一個隨機點,並發現提升提供了完全這樣的分佈。但是當我嘗試使用它時,所有生成的值都是nan。我不知道我做錯了什麼,請問您能否賜教?這個小代碼描述了我想做的事:如何使用boost :: uniform_on_sphere?

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <boost/random/mersenne_twister.hpp> 
#include <boost/random/uniform_on_sphere.hpp> 

int main() 
{ 
    boost::mt19937 gen; 
    boost::uniform_on_sphere<float> dist(3); 

    { // Seed from system random device 
    std::ifstream rand_device("/dev/urandom"); 
    uint32_t seed; 
    rand_device >> seed; 

    gen.seed(seed); 
    } 

    while(1) { 
    // Generate a point on a unit sphere 
    std::vector<float> res = dist(gen); 

    // Print the coordinates 
    for(int i = 0; i < res.size(); ++i) { 
     std::cout << res[i] << ' '; 
    } 
    std::cout << '\n'; 
    } 
} 

輸出是:

nan nan nan 
nan nan nan 
nan nan nan 
nan nan nan 

循環往復......

+0

代碼爲我工作正常(使用boost 1.48,當然沒有無限循環)。我認爲它是你的boost版本中的一個bug。 – 2012-04-18 04:00:46

回答

2

我假設你正在使用Boost 1.49。下面的方法適用於這種情況。

這是Boost有點複雜的地方。該對象是uniform_on_sphere只是您繪製點所需的一個組件,它是分佈的一部分。您還需要創建一個boost::variate_generator,沿着以下工作.cpp文件的行。

請注意,這有我自己的包括使用系統時間種子等,你應該能夠修改這個以適應你。

// Standards, w/ctime to seed based on system time. 
#include <iostream> 
#include <fstream> 
#include <vector> 
#include <ctime> 

// Boost. Requires variate_generator to actually draw the values. 
#include <boost/random/mersenne_twister.hpp> 
#include <boost/random/uniform_on_sphere.hpp> 
#include <boost/random/variate_generator.hpp> 


int main(){ 

    typedef boost::random::mt19937 gen_type; 

    // You can seed this your way as well, but this is my quick and dirty way. 
    gen_type rand_gen; 
    rand_gen.seed(static_cast<unsigned int>(std::time(0))); 

    // Create the distribution object. 
    boost::uniform_on_sphere<float> unif_sphere(3); 

    // This is what will actually supply drawn values. 
    boost::variate_generator<gen_type&, boost::uniform_on_sphere<float> > random_on_sphere(rand_gen, unif_sphere); 

    // Now you can draw a vector of drawn coordinates as such: 
    std::vector<float> random_sphere_point = random_on_sphere(); 

    // Print out the drawn sphere triple's values. 
    for(int i = 0; i < random_sphere_point.size(); ++i) { 
     std::cout << random_sphere_point.at(i) << ' '; 
    } 
    std::cout << '\n'; 

    return 0; 
} 

當我在控制檯中運行,我得到看似正確的東西:

[email protected]:~/Desktop/Programming/C++/RandOnSphere$ g++ -I /usr/include/boost_1_49/ -L /usr/include/boost_1_49/stage/lib randomOnSphere.cpp -o ros 
[email protected]:~/Desktop/Programming/C++/RandOnSphere$ ./ros 
0.876326 -0.0441729 0.479689 
[email protected]:~/Desktop/Programming/C++/RandOnSphere$ ./ros 
-0.039037 -0.17792 0.98327 
[email protected]:~/Desktop/Programming/C++/RandOnSphere$ ./ros 
-0.896734 0.419589 -0.14076 
+0

發現它,不連貫地使用。 – lvella 2012-04-18 04:21:53

+0

N/m我明白了。在我最上面的代碼部分,我確實有一個錯誤(現在編輯)。儘管如此,這是底部的代碼。謝謝你的提示。 – ely 2012-04-18 04:23:50

+0

在文檔中它指定哪些分佈可以直接使用,什麼需要variate_generator?本教程中的所有示例均直接與生成器一起使用分佈。 – lvella 2012-04-18 04:48:07

相關問題