python - Do I need custom random-number generator for Perlin noise? -


perlin explained in pseudo-code: http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

the tutorial gives me random number generator function writen in pseudo-code. returns floating number in range of (-1, 1).

function intnoise(32-bit integer: x)                  x = (x<<13) ^ x;     return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);     end intnoise function 

so if function returns number in range (-1, 1), can't use random.uniform(-1, 1)? meet problem:

function noise(x) . . end function  function smoothnoise_1d(x)     return noise(x)/2  +  noise(x-1)/4  +  noise(x+1)/4 end function 

i guess noise(x) function generates random numbers 1d noise.

i can't seem understand x parameter is. seed? and, can't use random.uniform(-1, 1)

the noise function used in perlin noise seeded random number generator. is, must return same value every time called same value parameter, x. can think of x position in space in given dimension between bounds of region you're computing perlin noise over.

you can use python random module if can reset state of rng based upon given parameter returns same value given x.

import random  rand_state = random.random()  def noise(x):   rand_state.seed(x)   return rand_state.random()  >>> noise(1) 0.13436424411240122 >>> noise(2) 0.9560342718892494 >>> noise(1) 0.13436424411240122 

note noise returned same value when passing 1 in first time, , second. returned different value when value other 1 input. parameter seed can hashable type in python. purposes, numeric type works.

typically when creating perlin noise, many calls made noise function, you'll want fast. on machine, takes 14 microseconds execute function above. that's ~70000 calls per second. may implementing pseudocode intnoise may result in better performance. infact, following method:

max_int = (1<<31)-1 def intnoise(x):     x = int(x)     x = ((x << 13) & max_int) ^ x     x = ( x * (x * x * 15731 + 789221) + 1376312589 ) & max_int     return 1.0 - x / 1073741824.0 

seems take on average 1.6 microseconds per invocation, or 10 times faster noise above. range of return values is (-1, 1), can changed modifying last line. can't speak uniformity of distribution, however, picture worth thousand words. blue dots below intnoise, , red dots python random.uniform function.

plot of intnoise

the noise function above can used smooth noise algorithm in question. url linked in question describes smoothing functions better could. after reading paragraph, study pictures of 1d , 2d smoothing next better understand purpose.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -