A random terrain generator with a Linear Congruential Generator to generate pseudo-random numbers that give a bit more organic result.
(function(){function k(b,h,f){f=f*n()-f/2;b=(b+h)/2+f;if(mh>b)mh=b;if(xh<b)xh=b;return b}function u(b,h,f,j){var q=o((b+f)/2),p=o((h+j)/2);g[q][h]=k(g[b][h],g[f][h],f-b);g[b][p]=k(g[b][h],g[b][j],j-h);g[f][p]=k(g[f][h],g[f][j],j-h);g[q][j]=k(g[b][j],g[f][j],f-b);g[q][p]=k(g[b][p],g[f][p],f-b)}d=s=512;c=document.body.children[0];t=c.getContext("2d");g=[];r=513;mh=255;xh=0;M=Math;o=M.round;m=4294967296;a=1103515245;cc=12345;for(n=new (function(){state=M.floor(M.random()*(m-1));return function(){return(state=
(a*state+cc)%m)/(m-1)}});r--;){g.push([]);for(sd=d+1;sd--;)g[g.length-1].push(0)}for(;s>1;){for(y=0;y<d;y+=s)for(x=0;x<d;x+=s)u(x,y,x+s,y+s);s=o(s/2)}l=255/(xh-mh);c.width=d;c.height=d;img=t.createImageData?t.createImageData(d,d):t.getImageData(0,0,d,d);pix=img.data;for(x=d;x--;)for(y=d;y--;){e=o((g[x][y]-mh)*l);cl=e<100?[,,12]:e<120?[6,6,16]:e<130?[12,11,9]:e<160?[6,12,6]:e<180?[6,16,6]:e<200?[5,6,30]:e<220?[9,9,6]:[16,16,16];for(i=4;i--;)pix[x*4+y*d*4+i]=i>2?255:cl[i]*16}t.putImageData(img,0,0)})();
KGZ1bmN0aW9uKCl7ZnVuY3Rpb24gayhiLGgsZil7Zj1mKm4oKS1mLzI7Yj0oYitoKS8yK2Y7aWYobWg+YiltaD1iO2lmKHhoPGIpeGg9YjtyZXR1cm4gYn1mdW5jdGlvbiB1KGIsaCxmLGope3ZhciBxPW8oKGIrZikvMikscD1vKChoK2opLzIpO2dbcV1baF09ayhnW2JdW2hdLGdbZl1baF0sZi1iKTtnW2JdW3BdPWsoZ1tiXVtoXSxnW2JdW2pdLGotaCk7Z1tmXVtwXT1rKGdbZl1baF0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XRhKGltZywwLDApfSkoKTs=
(function() {
d=s=512,c=document.body.children[0],t=c.getContext("2d"),g=[],r=513,mh=255,xh=0,M=Math,o=M.round,
m = 0x100000000, a = 1103515245, cc = 12345,
n = new (function() {
state = M.floor(M.random() * (m-1));
return function() {
// returns in range [0,1]
return (state = (a * state + cc) % m) / (m - 1);
}
})
while(r--) {
g.push([]);
sd=d+1;
while(sd--)
g[g.length-1].push(0);
}
while(s>1) {
for(y=0;y<d;y+=s)
for(x=0;x<d;x+=s)
subdivide(x,y,x+s,y+s);
s = o(s/2);
}
l = 255/(xh-mh);
function getheight(z0, z1, dl) {
var fc = dl*n()-dl/2,
e = (z0+z1)/2 + fc;
if(mh>e) mh=e;
if(xh<e) xh=e;
return e;
}
function subdivide(x0, y0, x1, y1) {
var xn = o((x0+x1)/2), yn = o((y0+y1)/2);
g[xn][y0] = getheight(g[x0][y0], g[x1][y0], x1-x0);
g[x0][yn] = getheight(g[x0][y0], g[x0][y1], y1-y0);
g[x1][yn] = getheight(g[x1][y0], g[x1][y1], y1-y0);
g[xn][y1] = getheight(g[x0][y1], g[x1][y1], x1-x0);
g[xn][yn] = getheight(g[x0][yn], g[x1][yn], x1-x0);
}
c.width=d;
c.height=d;
img=t.createImageData?t.createImageData(d,d):t.getImageData(0,0,d,d), pix=img.data;
x=d;while(x--) {
y=d;while(y--) {
e=o((g[x][y]-mh)*l), // e = ld elevation
cl = e<100?[,,12]:e<120?[6,6,16]:e<130?[12,11,9]:e<160?[6,12,6]:e<180?[6,16,6]:e<200?[5,6,30]:e<220?[9,9,6]:[16,16,16];
i=4;while(i--)
pix[x*4+(y*d)*4+i] = i>2?255:cl[i]*16;
}
}
t.putImageData(img,0,0);
})();