Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Question about Perlin Noise number generator

This topic is 5357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hey all!! Atm, I''m working on procedural textures, and I read this site: http://www.animeimaging.com/asp/PerlinNoise.aspx I understand what is explained there, but I''ve got a question about a function (which can be found at the site I mentioned): float noise::Noise2d(int x, int y) {    int n;    n = x + y * 57;    n = (n<<13) ^ n;    float res = (float)( 1.0 - ( (n * (n * n * 15731 + 789221)                            + 1376312589) & 0x7fffffff ) / 1073741824.0);    return res; } I would like to know, and understand how the math of this function works, so I can create my own version of it. Only thing the site says, is that it are prime numbers. Can somebody help me, or point me to a site where this (kind of) function is explained? Thanks!! GitTech

#### Share this post

##### Share on other sites
Advertisement
Since n is a signed int... it has a range of (nearly) +/- 2^31. ie +/- 2147483648. Essentially all that is hapenning, is a number of operations are carried out on the number, specifically to overflow that range... This is to exploit the chaos inherent in many modulo multiplication problems (eg the doubling map). This multiplication and overflow (similar to multiplying that carrying out a modulo operation) leads to a number which bears very little resemblance to the inputted numbers. The nature of the operation, becasically means that any two adjacent values are apparently unrelated (ie values for adjacent x/y). This is obviously not the case, as this is a deterministic system, but you get my point. In cases like this (mad calculations to get pseudo random numbers) the result may or may not be strictly uniformly distributed, but this is not usually an issue in a lot of situations. In any case, the intent is to map from x,y pairs to a signed int, anywhere in the allowable domain. This number is then divided by 2^30... this results in a number between -1 and 1... then it is subtracted from 1.... ie it results in a number between 0 and 2. oops.. oh no it doesn't hehehe.. I just noticed that & step. That makes sure that the number is strictly positive... ie the resulting number after the division is between 0 and 1... thus 1 - that number is between 0 and 1. Basically it has no specific mathematical basis other than it being vaguely related to a system that is known to be chaotic.. and thus may produce apparently (pseudo) random numbers. It is by no means guaranteed to produce every value within the co-domain (ie the +ve signed integers)... but it'll probably be random enough for many purposes (fractal noise being one I suspect).

EDIT: I believe the numbers are prime, as otherwise multiplication would result in a factor being common to all genarated numbers... this is obviously not desirable.

[edited by - dmounty on February 21, 2004 4:17:14 PM]

#### Share this post

##### Share on other sites
The function returns a pseudo-random number in the range [-1,1] based on the inputs x and y. I don't why it goes through so much computation just to achieve that. There can be a problem because "n = x + y * 57" may create an obvious pattern.

dmounty: 231 / 230 = 2, so the range is [ 0, 2 ], and 1 minus that is [ -1, 1 ].

Note also that the actual range of the integer part is [ 0, 231-1 ] and ( 231-1 ) / 230 is almost 2. But that number may or may not round to 2. It depends on precision errors in the divide and conversion, and the rounding mode of the processor. So , -1 can be generated on some processors, but never be generated on others.

[edited by - JohnBolton on February 21, 2004 4:42:27 PM]

#### Share this post

##### Share on other sites
That function is based on a distorted version of perlin noise. It is generating a number by using x and y to seed a random number generator and taking the first number returned by the random number generator.

Perlin''s method actually initializes a 2d array using random numbers and then the Noise2d function would just return the number stored at (x,y) in the array. That creates more random noise, and with less computation required.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19
• Advertisement

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632999
• Total Posts
3009812
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!