   Perlin Noise Algorithm
  I'm trying to make a Perlin Noise algorithm in Lua for generating terrain. I think I understand how it works, and have converted this algorithm to Lua and written some code to display it.Problem: This function to generate a random number (from the above link.)  function Noise1(integer x, integer y)    n = x + y * 57    n = (n<<13) ^ n;    return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0); end functionLua doesn't have bitwise operators built in so that will not work. I tried to use the LuaBit class, but either the class or the function is bugged because some of the operations would return extremely large numbers which would be turned into 0, resulting in the function resulting 0 nearly every time.I don't have access to C libraries (this project is just script for a game) and am only taking Trig... so can someone help me create a function that: takes parameters x, y, and preferably a random seed, and returns a pseudorandom, floating-point number between -1 and 1.Pseudocode would be fine if you don't know Lua.Thanks!   
  You don't need to use bitwise operators, it's just a faster way of doing things but considering you're using lua for perlin noise, I'm guessing speed is not important to you.(n << 13) is the same thing as (n * 2^13)the (x & 7fffffff) operation is equivalent to (x mod 2147483648)[edit]I should mention that in the above function the '^' represents power, not xor.In the original perlin function, n = (n<<13) ^ n, '^' is an XOR.   ;  Anyone see any obvious mistakes in my implementation of the suggestions above (thanks)?function noise1(x, y)n = x + y * 57n = bxor((n * 2^13), n);return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) % 2147483648) / 1073741824.0)end(bxor is a function from the Lua Users mailing list, I tested and it definitely works correctly.It seems like it is just increasing the height of the terrain as y increases (eventually going over 1), with some slight variation.   ;Quote:Anyone see any obvious mistakes in my implementation of the suggestions above (thanks)?As far as a psuedo-random noise function goes, it looks fine to me. Though, generally you would only take 1 input parameter for 1-dimensional noise, so you really have a noise2 function there... but that's really not important.(in C++)inline float NoiseWrapper::integerNoise1(int x){x += xSeed;x = (x << 13) ^ x;return (1.0f - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) * 0.000000000931322574615478515625f);}inline float NoiseWrapper::integerNoise2(int x, int y){x += xSeed; y += ySeed;x += y * 47;x = (x << 13) ^ x;return (1.0f - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) * 0.000000000931322574615478515625f);}inline float NoiseWrapper::integerNoise3(int x, int y, int z){x += xSeed; y += ySeed; z += zSeed;x += (y * 47) + (z * 59);x = (x << 13) ^ x;return (1.0f - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) * 0.000000000931322574615478515625f);}Do you have the rest of the implementation? If you're trying to get Perlin noise, you need to take these discreet noise values and do a gradient interpolation to create continuous noise. Actually... Ken Perlin does not use a noise function at all in his implementation, he simply uses lookup tables.float NoiseWrapper::perlinNoise2(float x, float y){int ix = math.f2iRoundFast(x - 0.5f);int iy = math.f2iRoundFast(y - 0.5f);x -= ix; y -= iy;float x1 = x-1.0f; float y1 = y-1.0f;ix &= 255; iy &= 255;float s = math.qCurve(x);float t = math.qCurve(y);int a = p[ix  ]+iy;int b = p[ix+1]+iy;returnmath.lerp(math.lerp(grad(p[a  ],x ,y ),    grad(p,x1,y ), s),<br>  math.lerp(grad(p[a+<span class="cpp-number">1</span>],x ,y1),<br>    grad(p[b+<span class="cpp-number">1</span>],x1,y1), s), t) * PERLIN2_MULT;<br>}<br><br></pre></div><!–ENDSCRIPT–> ;  It should be a 2D noise function, since I am generating terrain.I just used the pseudocode from the above link and converted it to Lua, then used a simple x/y loop to generate a heightmap:for x=0, sizeX doheightmap[x] = {}for y=0, sizeY doheightmap[x][y] = perlin2D(x, y)endendI think it works because while I was experimenting with random seeds to generate the noise, occasionally I would get a seed that produced something that looked sort of like terrain rather than bizarre patterns.Then it was just some simple code to display the terrain.I didn't know about the lookup table method... I'll try that.   ;  The point I'm trying to make is that your noise function is not Perlin noise. It's just pseudo-random integer noise. It's not suitable for any graphical effect as-is, including terrain. You need to interpolate the integer noise to attain continuous (a.k.a coherent) noise before it can be used.Also, the "lookup table method" isn't just a method, it IS Perlin noise in every sense of the title. Ken Perlin's implementation of gradient noise using lookup tables IS "perlin noise."For more information, I suggest you go here and read if you haven't already.   ;  That reference does not show actual Perlin Noise.  It's a major annoyance I had to get over when I first learned about Perlin Noise.  That blasted article ran me in circles looking for something that wasn't even what I wanted.  Irritatingly, it's always the first thing people find when they look up the subject.This is Perlin Noise.  What you're looking at is actually called value noise, IIRC.If you need real Perlin Noise (slower, but there are substantial advantages to it), here is a good page describing the algorithm, from Perlin himself.It's much more complicated as well.   ;Quote:What you're looking at is actually called value noise, IIRC.this page has a section covering gradient noise as well. You just need to do a little digging. People hear "Perlin noise" this and "Perlin noise" that and think it's the end-all be-all of noise, but I don't even use it anymore. Simplex noise is much faster and produces more consistent visual results in n-dimensions when compared to improved Perlin noise (see screenshot of 3d improved perlin noise at z=0.5). Ken Perlin himself wrote about simplex noise being superior and has even provided his own implementation to the public... should this also be called Perlin noise?? Hmm. Starting out, you were probably just as confused as the OP was, which is why you just went hunting for Perlin noise to begin with and got annoyed when you found libnoise docs instead... that is exactly why I didn't send the OP directly to Perlin's page.What good is a Perlin noise implementation if you don't even understand the basics of procedural noise in the first place? And, one should also realize that Perlin noise is just an optimized implementation of general "gradient" noise that uses lookup tables in a clever way to improve performance. Sending the OP directly to Ken Perlin's page accomplishes neither of these tasks. The link I provided is one of the best sources on the net that I've found explaining the progression from basic to advanced.3d improved Perlin noise at z=0.0 - Good3d improved Perlin noise at z=0.5 - Not so good3d simplex noise at z=0.0 - Good3d simplex noise at z=0.5 - Still goodSo now I ask... why settle for Perlin noise?end noise rant, sorry...   