I am in need of a way to randomly split a 1-dimensional range of values (i.e. continuous integers) into k sections. Just using a pseudo-random generator to pick split points would technically get the job done. However it allows for possibility of ranges either being very tiny (conversely very large). I’ve been looking for a way to generally solve this problem without resorting to hard-coded range limits.
I have found this article. It is concerned with 2d terrain generation. But it faces the same issue and presents a solution. You can see the Polygons section, where the author mentions Lloyd relaxation. What that whole thing derives from is Voronoi diagrams, and it works with 2d ranges. Furthermore, if you look at the algorithm for building the Voronoi diagram that Lloyd relaxation needs, it starts with:
let *(z) be the transformation *(z) = (zx, zy + d(z)), where d(z) is a parabola with minimum at z
Naturally, I don’t have parabolas in 1d.
It is not clear to me how to achieve the same results in my case of 1d range. Or maybe there is a different/better approach to the problem?
I didn’t get into the details of his code, but what he did with Voronoi diagrams in 2d, then choosing the center of the polygons as new points and remaking the Voronoi diagrams gives me this idea:
Let’s follow this by an example. For simplicity, let’s assume we are working on continuous ranges.
So you start with range [0, 80] and you want to divide it in 15 ranges.
Let’s say your 15 random numbers after being sorted are (generated by a C program:)
The midpoints are:
So your ranges become:
If you want to visualize this, it looks like this (as best I can show it in text):
Where the
.show numbers from 0 to 80 and+show the edges of Voronoi ranges.Now, let’s apply step 3.
So let’s see now see how the Voronoi ranges look like with the new points:
Now your ranges are (it’s starting to look ugly, but bear with me)
So now let’s take a look at how this distribution of points looks like:
Now let’s compare the two distributions:
Looks better, doesn’t it? That’s exactly what he has done in the article you found with 2d Voronoi polygons applied to 1d ranges.
(Excuse me for any possible computation errors)