I have seen many noise implementations for things like perlin
and simplex noise
to achieve procedural terrain generation
, and they all seem to use 256 permutation values. Why is this? and what would be the effect of using more than 256?
Asked
Active
Viewed 256 times
0

Kelan
- 355
- 4
- 16
-
Could you give more context? What application or codebase do you see this in? – Nayuki Jul 23 '15 at 16:46
-
4Because it's the number of unique values in a byte. – Colonel Thirty Two Jul 23 '15 at 16:50
-
A lot of terrain heightmaps are specified by 8-bit greyscale images so this is the expected number of values you would need to populate one. The "effect of using more than 256" would be that you have to either quantize your results or find a way to use higher color resolution for your heightmaps :p – Darren Ringer Jul 23 '15 at 17:14
1 Answers
2
You could use any number of permutation values. The reason powers of two are preferred is, because it is cheaper to compute the modulus of N^2.
The reason for that is:
value % (N^2)
is equivalent to
value & (N^2 - 1)
And it is much cheaper to compute a bitwise & instead of a %.
For example in your code you could write this:
int v = perm[(x + perm[y % 256]) % 256];
Or
int v = perm[(x + perm[y & 255]) & 255];
Both give you the same result but the second method is faster.
Of course any power of two can be used for this. I think the reason for choosing 256 is that it is a good balance between variety of pseudo random numbers and low memory consumption.

bakkaa
- 673
- 6
- 25