Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7833023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:32:17+00:00 2026-06-02T12:32:17+00:00

I am trying to pick a random point on a unit sphere, and found

  • 0

I am trying to pick a random point on a unit sphere, and found that boost provides the distribution that does exactly this. But when I try to use it, all generated values are nan. I don’t know what I am doing wrong, could you please enlighten me? This small code depicts what I am trying to do:

#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';
  }
}

The output is:

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

ad infinitum…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:32:19+00:00Added an answer on June 2, 2026 at 12:32 pm

    I’m assuming you are using Boost 1.49. The method below works in that case.

    This is where Boost is a little complicated. The object that is uniform_on_sphere is just one component of what you need in order to draw points, it is the distribution part.. You also need to create a boost::variate_generator along the lines of the following working .cpp file below.

    Note, this has my own includes for using the system time to seed, etc. You should be able to modify this to suit you.

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

    When I run this in the console, I get seemingly correct stuff:

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pick up Lua programming but I'm stuck on something that's probably
OK, this is minefield, but trying to understand why one would pick .NET (or
I'm trying to pick a random element from an array -- is this possible
I am trying to use 'get_posts' to pick up posts that are in two
I am trying to get jquery to pick up variable that I am defining
I'm trying to figure out a way to create random numbers that feel random
What is the pythonic way to perform this loop. I'm trying to pick a
Hey im trying to create a random way to pick a team of 4
I am trying to use random function in C# to randomly pick four from
I'm trying to make a simple javascript application to pick a random number between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.