# How can I generate random 3d points?

This topic is 4947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having trouble making random 3d points. is there any better way to make random numbers than rand() ?

##### Share on other sites
Yes (in terms of better 'quality' random numbers), but most likely the difference won't be noticable. What do you mean 'having trouble'? Just do:
const int RANGE = 5; //generate points with all coordinates between -5 and 5point.x = 2.0f * ((float)rand() / MAX_RAND) - 0.5f) * RANGE;point.y = 2.0f * ((float)rand() / MAX_RAND) - 0.5f) * RANGE;point.z = 2.0f * ((float)rand() / MAX_RAND) - 0.5f) * RANGE;

##### Share on other sites
rand() should work just fine, although if you want a faster randomizer (and it appears "more random", from what I've heard), you could make a Mersenne Twister implementation.

##### Share on other sites
Do you want vectors with a length of 1 and a uniform distribution?

I forgot the formula for a sphere, but for a circle you pick a random angle and pick a radius using "max radius * sqrt(random(0.0, 1.0))" presuming random gives a uniform distribution.

##### Share on other sites
Quote:
 Original post by ExtrariusDo you want vectors with a length of 1 and a uniform distribution?I forgot the formula for a sphere, but for a circle you pick a random angle and pick a radius using "max radius * sqrt(random(0.0, 1.0))" presuming random gives a uniform distribution.

To generalize to n dimensions, the easiest (but probably not fastest) way is to pick a random value for each direction using a normal (i.e. Gaussian) distribution, and then normalize the result.

To generate such distributions, [google] "Polar Box-Mueller".

##### Share on other sites
Using Halton sequences is generally the best way to distribute points because it "fills the space" in an efficient manner. It is used for Quasi-MonteCarlo techniques, including illumination problems in computer graphics. Here is the link to the theory and source code:

http://www.csit.fsu.edu/~burkardt/cpp_src/halton/halton.html

-cb

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632921
• Total Posts
3009210
• ### Who's Online (See full list)

There are no registered users currently online

×