Pseudorandom number generator for noise

前端 未结 3 1702
感动是毒
感动是毒 2021-01-16 03:27

I\'m trying to make the Perlin noise algorithm described at http://freespace.virgin.net/hugo.elias/models/m_perlin.htm using Lua. However, it doesn\'t work properly since Lu

3条回答
  •  粉色の甜心
    2021-01-16 03:58

    It is easy to make a linear congruential random number generator in Lua. A simple one is Park-Miller

    function pmrng (x) return math.fmod(x * 16807, 2147483647) end
    

    This will give you the next random integer [1..2147483646] after x, the seed. Use this integer to make a float by dividing by the modulus, 2147483647 in this case.

    prng_seed = 13579
    function upmrng () prng_seed = pmrng(prng_seed); return prng_seed / 2147483647 end
    

    To scale this to -1 .. +1 do

    upmrng() * 2 - 1
    

提交回复
热议问题