Uniform feature points

Main/Procedural content


[h2]Uniform Feature Points[/h2]
Let us imagine that we have a 100x100 meter field and in this field we what there to be, on average, two flowers per meter squared. So we could create an array with 100x100x2 = 20,000 elements to explicitly store the positions of each flower. Using a seeded uniform random number generator we could then fill the array with repeatable coordinates for each. If we were to examine the placement of flowers we would notice that there would be regions with none and areas where they are clummped up.


for(int i=0; i<100*100*2; i++) {
  float x = 100*rng.nextFloat();
  float y = 100*rng.nextFloat();

Now if were to examine each square meter (or some other regular chunk) and count the number of flowers it contains, then the “distribution” of the counts approaches the Poisson distribution (Wikipedia, MathWorld).

So instead of precomputing a explicit list of “features”, the space in question can be broken down into parts. For each part one computes a poisson random number to determine the number of features inside it.

// From Knuth, reasonable on modern machines for smallish
// means.  There are many ways this can be computed.
// eMean = exp(-mean)
public static final int poisson(float eMean)
  int   r = 1;
  float t = rng.nextFloat();
  while (t > eMean) {
    t *= rng.nextFloat();
  return r-1;

// Sketch below this point
private static final float FLOWER_POWER = (float)Math.exp(-2);

private void doSomeCellFlowerThing(...)

  int num = poisson(FLOWER_POWER);

  for(int i=0; i<num; i++) {
    // coordinates local to the square meter in this example
    float x = rng.nextFloat();
    float y = rng.nextFloat();

Assuming that the chosen hashing function does a reasonable job, then the “look” of the localized vs. global versions should be similar. Up until now I’ve avoided too much techo-speak but some definitions are required. All of the above rng.nextFloat() calls are assume to return a uniformly distributed random number on [0,1) which is the standard base contract for a uniform floating point result (actually the inclusion/exclusion of the end-points may vary, but I’m assuming the presented). In probability a uniform distribution (Wikipedia, MathWorld) means that all results on the range are equally probable…like in a single fair die roll. So in the examples above the ‘x’ coordinate of each flower is completely independent of ‘y’, which should be expected. Moreover the coordinates of each flower is independent of any other. This leads to the previously mentioned empty and clumpped up areas. This result, like many from probability, is likely to seem counter-intuitive. Most likely “uniform” will tend to invoke notions of uniformly (or evenly) covered areas. This is a radially different notion of uniform as implies that all of the values are related to each other rather than independent. Note that these two different notions of uniform approach one another as the number of features (or event) increase.

for things like this I really like the halton sequence

maxis did some nice things in spore with this(point 3 url=http://http://www.cs.cmu.edu/~ajw/s2007/

I did an implementation of this here: https://github.com/Danny02/DarwinsBox/tree/master/Util/src/main/java/darwin/util/math/halton

quasi-random numbers do work pretty well…the complexity tends to be higher…unless I’m missing something. The effect is different, but as long as it looks good…that’s all that matters.

the halton sequence has some very nice deterministic properties which are explained very well in the linked presentation

while creating new elements of the sequence, the distribution is always “uniform”

Basically the difference is that quasi-random will give better coverage, which is more regular…less clumps & empty areas. Actually the wikipedia picture gives pretty good idea: http://en.wikipedia.org/wiki/Halton_sequence

[quote]So instead of precomputing a explict list of “features”, the space in question can be broken down into parts. For each part one computes a poisson random number to determine the number of features inside it.
Then what? Once you know how many flowers are in a certain grid location, how do you distribute them within it? What do you do about edges and corners?

They are uniformly distributed inside…I’m going to add some pseudo code for the example. This is the technique that point-noise based functions (like cellular noise) is based on…for instance.

Updated the text…does that answer you’re question?

According to that code they are randomly distributed within a cell, not uniformly distributed inside of it. A group of four cells could potentially could have a greater density of flowers around their shared corner and much lower density everywhere else. The same could apply to edges with two cells. Within one cell, multiple flowers could clump together.

They need to be uniformly distributed with respect to other objects in the same cell as well as to those in neighboring cells.

BUE & Danny02: you’re both using a non-probability based notion of uniform or more specifically a uniform random process. Added some text to deal with the notion of “uniform”.

All these examples and opposite versions of them are likely and desirable. It’s exactly what’s suppose to happen. If you were to create two images with points, one globally and the other by breaking into parts and using poission and randomly choose to display one on the right and left…you shouldn’t be able to identify which is which. If you can, then the hashing isn’t working.

The various test-suites of PRNG quality will perform many operations in the opposite direction from this. Create a bunch of points in ‘n’ dimensions, then break that space up into various sized parts…count the number of contents inside each part and if the values don’t approach the Poisson distribution, then it fails the test…the PRNG is not creating uniform random numbers in that number of dimensions.

I see. You’re talking about emulating a process where each point’s location coordinates are selected at random with uniform probability. Not that the generated graphics look uniformly distributed. You had me confused when you were talking about the Halton sequence.

Or in other words, uniform like rolling a fair dice twice and plotting the values as x-y coordinates - but not uniform like a uniform distribution of points.