views:

110

answers:

7

Say you have a 2D area and you want to generate random points within it, by setting

x = random() * width 
y = random() * height 

do the points clump around the centre of the area? I remember reading something saying they would, but I can't quite figure out why, and how to prevent it.

+4  A: 

It depends on the distribution of the random number generator. Assuming a perfectly even distribution, then the points are likely to be distributed in a reasonably uniform way.

Also, asking if they clump around the middle is pre-supposing that you don't have the ability to test this!

Gian
ha ha yes I just tested it with 100,000 points. they do form clumps, but not in the middle.
Iain
It wouldn't be random if they _didn't_ form clumps somewhere. As the points are random, the distances between them must vary as well. Hence, there must be some small distances = clump of points together. (This margin is of course too narrow for the truly marvelous proof )
MSalters
+1  A: 

From my experience, randomly generated points do not clump in the center of the area since every pixel of your screen has the same probability of being selected.

While numbers generated with random() are not truely random, they will be sufficent for putting objects randomly on your screen.

sum1stolemyname
+4  A: 

Truly random points will create clusters (or clumps) - it's the effect that can cause confusion when plotting real world data (like cancer cases) and lead to people thinking that there are "hot spots" which must be caused by something.

However, you also need to be careful when generating random numbers that you don't create a new generator every time you want a new number - this will use the same seed value which will result in all the values clustering about a point.

ChrisF
+1  A: 

If the random number generator's random() function yields a gaussian distribution, then yes.

Dave
+3  A: 

Yes. The fewer points you have, the more they will appear to form clusters.

To avoid this, you can use "stratified sampling". it basically means you divide your surface evenly in smaller areas and place your points in there.

For your example, you would divide the square in n*n subsquares. Each point would be placed randomly inside it's subsquare. You can even adjust the randomness factor to make the pattern more or less random/regular:

// I assume random() return a number in the range [0, 1).

float randomnessFactor = 0.5;
int n = 100;

for(int ySub=0; ySub<n; ++ySub){
    for(int xSub=0; xSub<n; ++xSub){

        float regularity = 0.5 * (1-randomnessFactor)

        x = regularity + randomnessFactor * random() + xSub / (float) (n-1);
        x = regularity + randomnessFactor * random() + xSub / (float) (n-1);

        plot(x, y);

    }
}

The reason this works is that you don't actually want randomness. (Clumps are random.) You want the points evenly spread, but without the regular pattern. Placing the points on a grid and offsetting them a bit hides the regularity.

geon
+1  A: 

You get a clump at the origin if you use polar coordinates instead of carthesian:

r = rand() * Radius;
phi = rand() * 2 * Pi;

The reason is that statistically, the circle r=[0,1] will contain as many points as the ring r=[1,2] even though the ring is three times larger.

MSalters
+1 for thinking out of the box and pointing me to an interesting - and unexpected- phenomenon
sum1stolemyname
A: 

They will not clump, but will form various interesting patterns, in 2d or 3d, depending on the generator you use.

Daniel Mošmondor