我假设您使用的是 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;
}
当我在控制台中运行它时,我得到了看似正确的东西:
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ g++ -I /usr/include/boost_1_49/ -L /usr/include/boost_1_49/stage/lib randomOnSphere.cpp -o ros
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ ./ros
0.876326 -0.0441729 0.479689
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ ./ros
-0.039037 -0.17792 0.98327
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ ./ros
-0.896734 0.419589 -0.14076