# How to do Tileable Perlin Noise in C++?

I'm currently making a game that is going to involve terrain generation. I've already tried terrain generation using FastNoise2, and it "worked" and honestly would be faster than the "Roll your own" I'm going to try, but it'd be fun to roll my own anyway. I managed to get normal 2D perlin noise working like so:

float Noise2D (int x, int y, int seed) {
Random::gen.seed(x + y * 57 + seed); // <-- Random::gen is an instance of std::mt19937
auto n = fmod(Random::RandomFloat(), 1); // <-- RandomFloat is a random range function; with no parameters it just makes the range 0-[whatever the default max float value in <random> is]
Random::gen.seed(seed);
return n;
}

float PerlinNoise2D(float x, float y, int seed) {
auto floor_x = floor(x), floor_y = floor(y);
auto s = Noise2D(floor_x, floor_y, seed);
auto t = Noise2D(floor_x + 1, floor_y, seed);
auto u = Noise2D(floor_x, floor_y + 1, seed);
auto v = Noise2D(floor_x +  1, floor_y + 1, seed);

auto frac_x = x- floor_x;
auto frac_y = y - floor_y;
auto value = s * (1 - frac_x) + t * frac_x;
value += (u - s) * frac_y * (1 - frac_x);
value += (v - t) * frac_x * frac_y;
return  value;
}


And to test it:

void TestPerlinNoise() {
int seed = Random::RandomFloat(0, 1000000);
const int SIZE = 96;
SDL_Surface* surface = SDL_CreateRGBSurface(0, SIZE, SIZE, 32, 0, 0, 0, 0);
//float test[SIZE][SIZE];
auto scale = (10.0f / float(SIZE));
for (int y = 0; y < SIZE; y++) {
for (int x = 0; x < SIZE; x++) {
//test[y][x] = PerlinNoise2D(x, y, seed);
auto noise = PerlinNoise2D(x * scale, y * scale, seed);
Uint32 color = RGBA((uint8_t)(noise * 255), (uint8_t)(noise * 255), (uint8_t)(noise * 255), 255);
SDLSetPixel(surface, x, y, color);
}
}
IMG_SavePNG(surface, "test.png");
SDL_FreeSurface(surface);
}



Which makes this image:

But how do I make the output of this function tileable?

• Do you want to tile Perlin noise or value noise? As explained in this answer, your implementation generates value noise rather than Perlin noise. We already have a question & some answers on making tileable Perlin noise. Your solution isn't wrong, rather you're asking about one thing & implementing a different thing (that can give similar output) & without context, we don't know how to proceed. Commented Oct 7, 2023 at 14:42