2012-04-18 20 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

コードは(無限ループせずにブースト1.48、そしてもちろんを使用して)私のために正常に動作します。私はそのブーストのあなたのバージョンのバグだと思う。 –

答えて

2

私はあなたがブースト1.49を使用していると仮定しています。その場合、以下の方法が有効です。

これはBoostが少し複雑なところです。 uniform_on_sphereのオブジェクトは、ポイントを描画するために必要なものの1つのコンポーネントです。配布部分です。以下の作業中の.cppファイルの行に沿ってboost::variate_generatorも作成する必要があります。

注:これには、シードなどのシステム時間を使用するための独自のインクルードがあります。これはあなたに合わせて変更する必要があります。

// 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

+0

N/mわかりました。私のトップコードのセクションでは、間違った(今編集中)がありました。しかし、それは動作する下部のコードです。先端をありがとう。 – ely

+0

そして、ドキュメントでは、どのディストリビューションを直接使用できるのか、そしてこのvariate_generatorが必要なのかを指定していますか?チュートリアルのすべてのサンプルでは、​​ジェネレータで直接分布を使用しています。 – lvella

関連する問題