# [Haskell-cafe] Re: Zippers, Random Numbers & Terrain

Thomas Conway drtomc at gmail.com
Wed Aug 1 19:20:26 EDT 2007

```On 8/2/07, apfelmus <apfelmus at quantentunnel.de> wrote:
> That concludes the infinite terrain generation for one dimension. For
> higher dimension, one just needs to use 2D objects instead of intervals
> to split into two or more pieces. For instance, one can divide
> equilateral triangles into 4 smaller ones. In fact, it doesn't matter
> whether the starting triangle is equilateral or not when using the
> midpoints of the three sides to split it into four smaller triangles.

Nice. The issue of the RNG running backwards was what made me realize
that rather than using StdGen in the nodes, if you simply number them
(Hmmm - the nodes are countably infinite :-)), you can then [e.g.] use
a cryptographic hash or similar to turn them into random numbers. You
can seed the hash to generate different terrains.

You may be interested that in some of the code I wrote for the right
angle isosceles triangle case, I got into precision problems. It turns
out that all the vertices lie on positions with coordinates that are
precisely sums of 2^-k (e.g. 0.5, 0.125, 0.625), yet each time you
subdivide, the scaling factor on the side length is sqrt 2/2. The
resultant rounding meant that instead of getting 0.5, I got
0.5000000003, or some such.

After pondering on this for a while, I realized instead of
representing the scale of the triangle as a Double, I could use
(Either Double Double), with Left x representing the scale x, and
Right x representing the scale x * sqrt 2 / 2. That way, all the
rounding problems can be made to go away. Well, not all of them -
after all Double has limited digits of mantissa, but down to quite
small scales, the arithmetic will be precise. Actually, you could use
(Either Rational Rational), except that performance would be [even
more] atrocious.

cheers,
T.
--
Dr Thomas Conway
drtomc at gmail.com

Silence is the perfectest herald of joy:
I were but little happy, if I could say how much.
```