Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uniformly Distributed points on a plane are getting clustered in java, why?

Tags:

java

random

I want to generate uniformly distributed circles/points/nodes on a *100 plane. For that I am using the Random() method in java. Specifically I am doing it in in the following manner:

Random r1=new Random();
  for(int i=0;i<100;i++){
  x=100*r1.nextDouble();
  y=100*r1.nextDouble();
} 

But the problem is that as I run the code over and over again, the nodes are not uniformly spaced on the plane, i.e., there are clusters of concentrations and some chunks of un-occupied space.

Any ideas, recommendations would be highly appreciated. The image belows shows a typical output with the clusters and the white spaces. The number of the circles are just the IDs of the circles. enter image description here

like image 346
OAH Avatar asked Oct 11 '13 06:10

OAH


2 Answers

I assume you mean a 100x100 unit plane, with 100 points.

A 10x10 grid overlaid over your plane, with 1 point per grid box means 100 evenly distributed points.

Place points in the center for exact uniformity that's pretty:

for(int i=0;i<100;i++){
    x = 5 + 10*(i/10);
    y = 5 + i % 10;
} 

Or for a little bit of jitter, randomize the location within each grid box:

Random r1=new Random();
for(int i=0;i<100;i++){
    x = 10*r1.nextDouble() + 10*(i/10);
    y = 10*r1.nextDouble() + i % 10;
} 
like image 81
Adam Avatar answered Nov 12 '22 10:11

Adam


If you want your random distribution to look more "even", that is you want to cover the space more evenly, you cannot use a completely uniform distribution, since it will contain "gaps", as @Adam pointed out.

You can use instead something called Low-discrepancy sequence: Halton sequence for instance, or Sobol sequence. As you can see in the Wikipedia example pictures, they avoid clusters and gaps you will have with uniform distributions.

like image 7
Flavio Avatar answered Nov 12 '22 09:11

Flavio