Porting classic Perlin noise to JavaScript: getting a periodic picture. What for? (JsFiddle example)

I am trying to port the classic Perlin Noise (src: http://mrl.nyu.edu/~perlin/doc/oscar.html#noise ) to JavaScript - for scientific purposes. I don't know why, but my code generates a periodic pattern instead of a random pattern.

You can find my full code with an example drawn on the canvas here (trimmed to one dimension): http://jsfiddle.net/YL77D/

I think the problem can be found in the function "noise1":

sx = s_curve(rx0);

u = rx0 * g1[ p[ bx0 ] ];
v = rx1 * g1[ p[ bx1 ] ];

return lerp(sx, u, v);

If my x-seed is 10.1, 10.2, 10.3, etc. I think u and v should be the same number (u should be based on 10, and v should be based on 11 - so sx is somewhere in between). I'm right? But in my code, u and v are always different.

Any ideas? Thank you very much.

+3
source share
1 answer

One problem may be the use of Math.Random:

p[i] = p[j = Math.floor(Math.random() % B)];

In javascript Math.Random, a floating point value between 0 and 1
randomis returned . In C, it returns an integer value from 0 to RAND_MAX.

p[i] = p[j = random() % B];
+1
source

All Articles