Random numbers within a sphere?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stavros Christoforou

    Random numbers within a sphere?

    Hello everyone,

    I was wondering if someone could help me with an issue I have in C++. I
    want to select random points within the volume of a sphere. I know how
    to get random numbers using srand() and rand(), but have no idea how to
    do that within a more complicated geometry. Any help would be greatly
    appreciated..

    Regards
    Stavros
  • Fabio Rossi

    #2
    Re: Random numbers within a sphere?

    Stavros Christoforou wrote:
    [color=blue]
    > Hello everyone,
    >
    > I was wondering if someone could help me with an issue I have in C++. I
    > want to select random points within the volume of a sphere. I know how
    > to get random numbers using srand() and rand(), but have no idea how to
    > do that within a more complicated geometry. Any help would be greatly
    > appreciated..[/color]

    You could represent any point of the sphere in spherical coordinates: a
    radius and two angles. A coordinate is a uniform variable (using rand()) in
    its [min,max] interval.

    Comment

    • Victor Bazarov

      #3
      Re: Random numbers within a sphere?

      Stavros Christoforou wrote:[color=blue]
      > I was wondering if someone could help me with an issue I have in C++. I
      > want to select random points within the volume of a sphere. I know how
      > to get random numbers using srand() and rand(), but have no idea how to
      > do that within a more complicated geometry. Any help would be greatly
      > appreciated..[/color]

      Select a random radius (between 0 and the sphere radius), a randon azimuth
      (0 through 2*Pi) and a random elevation (-Pi/2 through Pi/2) and then
      convert the three values from spherical coordinates into Cartesian (if
      that's your desired result). And please don't post off-topic questions
      here. Your question has nothing to do with C++ language and ought to be
      posted in comp.graphics.a lgorithms or comp.programmin g.

      V

      Comment

      • Phlip

        #4
        Re: Random numbers within a sphere?

        Fabio Rossi wrote:
        [color=blue]
        > Stavros Christoforou wrote:
        >[color=green]
        > > Hello everyone,
        > >
        > > I was wondering if someone could help me with an issue I have in C++. I
        > > want to select random points within the volume of a sphere. I know how
        > > to get random numbers using srand() and rand(), but have no idea how to
        > > do that within a more complicated geometry. Any help would be greatly
        > > appreciated..[/color]
        >
        > You could represent any point of the sphere in spherical coordinates: a
        > radius and two angles. A coordinate is a uniform variable (using rand())[/color]
        in[color=blue]
        > its [min,max] interval.[/color]

        Stavros might need evenly distributed numbers. A polar coordinate scheme
        would bunch numbers up around the center, at least.

        The fix is to bound the sphere with a cube, tangent on all faces, and find
        random points in the cube by finding random xyz points, each >= -r and < r,
        where r is the radius of the sphere. Then filter out all points laying
        outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.

        --
        Phlip
        Industrial XP: Extreme Programming, XP, Agile Software Development, Agile Methods.




        Comment

        • Ron Natalie

          #5
          Re: Random numbers within a sphere?

          [color=blue]
          >
          > Stavros might need evenly distributed numbers. A polar coordinate scheme
          > would bunch numbers up around the center, at least.[/color]
          How so? If the [0, sphere-radius] random number generator is uniform, so would
          the density with respect to the center.
          [color=blue]
          >
          > The fix is to bound the sphere with a cube, tangent on all faces, and find
          > random points in the cube by finding random xyz points, each >= -r and < r,
          > where r is the radius of the sphere. Then filter out all points laying
          > outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.[/color]

          That wouldn't be any better and a lot slower as a lot of samples would have
          to be generated, tested, and discarded.

          Comment

          • Victor Bazarov

            #6
            Re: Random numbers within a sphere?

            Ron Natalie wrote:[color=blue][color=green]
            >> Stavros might need evenly distributed numbers. A polar coordinate scheme
            >> would bunch numbers up around the center, at least.[/color]
            >
            > How so? If the [0, sphere-radius] random number generator is uniform,
            > so would
            > the density with respect to the center.
            >[color=green]
            >>
            >> The fix is to bound the sphere with a cube, tangent on all faces, and
            >> find
            >> random points in the cube by finding random xyz points, each >= -r and
            >> < r,
            >> where r is the radius of the sphere. Then filter out all points laying
            >> outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.[/color]
            >
            >
            > That wouldn't be any better and a lot slower as a lot of samples would have
            > to be generated, tested, and discarded.[/color]

            "A lot of samples"? The sphere is about half the volume of its
            circumscribing cube. So, less than half of all samples are going to
            be discarded. Doesn't seem like "a lot" to me. Of course, everybody
            has their own definition of "a lot". Is there a better way to produce
            points evenly distributed in the volume of a sphere?

            V

            Comment

            • Shezan Baig

              #7
              Re: Random numbers within a sphere?


              Ron Natalie wrote:[color=blue][color=green]
              > >
              > > Stavros might need evenly distributed numbers. A polar coordinate[/color][/color]
              scheme[color=blue][color=green]
              > > would bunch numbers up around the center, at least.[/color]
              > How so? If the [0, sphere-radius] random number generator is[/color]
              uniform, so would[color=blue]
              > the density with respect to the center.
              >[/color]


              No, the probability of being close to the center is higher than the
              probability of being around the edges.

              Comment

              • Stavros Christoforou

                #8
                Re: Random numbers within a sphere?

                [color=blue]
                > No, the probability of being close to the center is higher than the
                > probability of being around the edges.
                >[/color]

                May I ask why?
                [color=blue]
                >Stavros might need evenly distributed numbers. A polar coordinate
                >scheme would bunch numbers up around the center, at least.[color=green]
                >>
                >>How so? If the [0, sphere-radius] random number generator is uniform, so would the density with respect to the center.
                >>[/color][/color]


                The density would be selected from a pdf, eg

                s = - (log ((double) (rand()+ 1.0)/RAND_MAX))/sigmat

                (just a random function)

                Therefore my problem focuses more on how to select the points within the
                sphere, and my idea so far was similar to Philip's. However, I am sure
                something faster and more "code-correct" exists.

                Also, sorry if I posted this on the wrong group, but as I am creating
                the program on C++ I thought that this would be the appropriate place to
                ask questions.


                Stavros

                Comment

                • Shezan Baig

                  #9
                  Re: Random numbers within a sphere?


                  Stavros Christoforou wrote:[color=blue][color=green]
                  > > No, the probability of being close to the center is higher than the
                  > > probability of being around the edges.
                  > >[/color]
                  >
                  > May I ask why?
                  >[/color]


                  It might be a little hard to explain without graphical aid :)

                  But basically, to be around the center, you need to have a small
                  radius. The angles don't really matter here - as long as the radius is
                  small, then the point will be near the center.

                  To make the point closer to a particular edge, you need a bigger radius
                  *and* the correct angle (or direction, or whatever). So the
                  probability of this happening is less.

                  Hope this helps,
                  -shez-

                  Comment

                  • Karl Heinz Buchegger

                    #10
                    Re: Random numbers within a sphere?

                    Ron Natalie wrote:[color=blue]
                    >[color=green]
                    > >
                    > > Stavros might need evenly distributed numbers. A polar coordinate scheme
                    > > would bunch numbers up around the center, at least.[/color]
                    > How so?[/color]

                    The problem is that the mean distance between 2 neighboring points is
                    smaller near the center then it is at the circumference. So the number
                    of points near the center is packed tighter -> the point density (nr of points
                    per partial volume) is higher. Depending on the application this might
                    or might not be acceptable.
                    [color=blue]
                    > If the [0, sphere-radius] random number generator is uniform, so would
                    > the density with respect to the center.
                    >[/color]

                    [color=blue][color=green]
                    > >
                    > > The fix is to bound the sphere with a cube, tangent on all faces, and find
                    > > random points in the cube by finding random xyz points, each >= -r and < r,
                    > > where r is the radius of the sphere. Then filter out all points laying
                    > > outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.[/color]
                    >
                    > That wouldn't be any better and a lot slower as a lot of samples would have
                    > to be generated, tested, and discarded.[/color]

                    .... Yes, but it generates a uniform volume sampling of the sphere. Something
                    the radius/angle method doesn't do.

                    --
                    Karl Heinz Buchegger
                    kbuchegg@gascad .at

                    Comment

                    • Karl Heinz Buchegger

                      #11
                      Re: Random numbers within a sphere?

                      Shezan Baig wrote:[color=blue]
                      >
                      > Stavros Christoforou wrote:[color=green][color=darkred]
                      > > > No, the probability of being close to the center is higher than the
                      > > > probability of being around the edges.
                      > > >[/color]
                      > >
                      > > May I ask why?
                      > >[/color]
                      >
                      > It might be a little hard to explain without graphical aid :)
                      >
                      > But basically, to be around the center, you need to have a small
                      > radius. The angles don't really matter here - as long as the radius is
                      > small, then the point will be near the center.
                      >
                      > To make the point closer to a particular edge, you need a bigger radius
                      > *and* the correct angle (or direction, or whatever). So the
                      > probability of this happening is less.
                      >[/color]

                      What 'edges' are you talking about?

                      --
                      Karl Heinz Buchegger
                      kbuchegg@gascad .at

                      Comment

                      • Shezan Baig

                        #12
                        Re: Random numbers within a sphere?


                        Karl Heinz Buchegger wrote:[color=blue]
                        > Shezan Baig wrote:[color=green]
                        > >
                        > > Stavros Christoforou wrote:[color=darkred]
                        > > > > No, the probability of being close to the center is higher than[/color][/color][/color]
                        the[color=blue][color=green][color=darkred]
                        > > > > probability of being around the edges.
                        > > > >
                        > > >
                        > > > May I ask why?
                        > > >[/color]
                        > >
                        > > It might be a little hard to explain without graphical aid :)
                        > >
                        > > But basically, to be around the center, you need to have a small
                        > > radius. The angles don't really matter here - as long as the[/color][/color]
                        radius is[color=blue][color=green]
                        > > small, then the point will be near the center.
                        > >
                        > > To make the point closer to a particular edge, you need a bigger[/color][/color]
                        radius[color=blue][color=green]
                        > > *and* the correct angle (or direction, or whatever). So the
                        > > probability of this happening is less.
                        > >[/color]
                        >
                        > What 'edges' are you talking about?
                        >
                        > --
                        > Karl Heinz Buchegger
                        > kbuchegg@gascad .at[/color]


                        Sorry, I meant a point near the circumference.

                        Comment

                        • Karl Heinz Buchegger

                          #13
                          Re: Random numbers within a sphere?

                          Shezan Baig wrote:[color=blue]
                          >
                          > Karl Heinz Buchegger wrote:[color=green]
                          > > Shezan Baig wrote:[color=darkred]
                          > > >
                          > > > Stavros Christoforou wrote:
                          > > > > > No, the probability of being close to the center is higher than[/color][/color]
                          > the[color=green][color=darkred]
                          > > > > > probability of being around the edges.
                          > > > > >
                          > > > >
                          > > > > May I ask why?
                          > > > >
                          > > >
                          > > > It might be a little hard to explain without graphical aid :)
                          > > >
                          > > > But basically, to be around the center, you need to have a small
                          > > > radius. The angles don't really matter here - as long as the[/color][/color]
                          > radius is[color=green][color=darkred]
                          > > > small, then the point will be near the center.
                          > > >
                          > > > To make the point closer to a particular edge, you need a bigger[/color][/color]
                          > radius[color=green][color=darkred]
                          > > > *and* the correct angle (or direction, or whatever). So the
                          > > > probability of this happening is less.
                          > > >[/color]
                          > >
                          > > What 'edges' are you talking about?
                          > >[/color]
                          >
                          > Sorry, I meant a point near the circumference.[/color]

                          In this case your argumentation doesn't apply. If a sampled
                          radius is 80% of the speheres radius, then it will be 20% from
                          the surface, no matter which direction.


                          --
                          Karl Heinz Buchegger
                          kbuchegg@gascad .at

                          Comment

                          • Joseph Seigh

                            #14
                            Re: Random numbers within a sphere?

                            On Tue, 08 Feb 2005 14:18:56 GMT, Phlip <phlip_cpp@yaho o.com> wrote:
                            [color=blue]
                            > Fabio Rossi wrote:
                            >[color=green]
                            >> Stavros Christoforou wrote:
                            >>[color=darkred]
                            >> > Hello everyone,
                            >> >
                            >> > I was wondering if someone could help me with an issue I have in C++. I
                            >> > want to select random points within the volume of a sphere. I know how
                            >> > to get random numbers using srand() and rand(), but have no idea how to
                            >> > do that within a more complicated geometry. Any help would be greatly
                            >> > appreciated..[/color]
                            >>
                            >> You could represent any point of the sphere in spherical coordinates: a
                            >> radius and two angles. A coordinate is a uniform variable (using rand())[/color]
                            > in[color=green]
                            >> its [min,max] interval.[/color]
                            >
                            > Stavros might need evenly distributed numbers. A polar coordinate scheme
                            > would bunch numbers up around the center, at least.
                            >
                            > The fix is to bound the sphere with a cube, tangent on all faces, and find
                            > random points in the cube by finding random xyz points, each >= -r and < r,
                            > where r is the radius of the sphere. Then filter out all points laying
                            > outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.
                            >[/color]
                            That's not likely to be an even distribution since you're discarding samples.
                            The OP need to define what the set of "points" is in the fist place, which
                            would depend on the coordinate system chosen. The OP would also need to
                            define the function f(x, y), where x and y are "points", against which the
                            random distribution is being applied. It could be metric distance between
                            the points or something else entirely. The OP really needs to repost their
                            question in comp.programmin g where they deal with these kind of questions
                            rather than there where it's a little off topic.

                            --
                            Joe Seigh


                            Comment

                            • Stavros Christoforou

                              #15
                              Re: Random numbers within a sphere?

                              Joseph Seigh wrote:
                              The OP really needs to repost their[color=blue]
                              > question in comp.programmin g where they deal with these kind of questions
                              > rather than there where it's a little off topic.
                              >[/color]

                              I see. Thank you all for your replies, I'll give it a shot there!

                              Stavros

                              Comment

                              Working...