Docs / Randomness

Randomness: noise

Gradient and fractal noise, their settings, the range their values really use, thresholds, and the coordinates they accept.

Color legend plain property method

Noise gives a smooth value for every point of a plane: close points have close values, far points unrelated ones. Terrain heights, caves, biomes, ore veins and wind all start from it. Both noise types here are pure functions of their seed and the point, with no state, no table and no period, so any region of an endless world can be sampled in any order, from any thread, and gives the same values every time and on every machine. Names are colored by what they are: a property or a method; values and fields keep the plain color.

Gradient noise

GradientNoise is one layer of Perlin-style gradient noise. The plane is cut into square cells 1 unit wide, each corner of a cell gets a direction hashed from the seed and the corner, and Sample blends the four corners of its cell smoothly. The values go from −1 to 1, and are 0 at every integer point. Scale the coordinates to choose how large the features are:

var wind = new GradientNoise(world.Fork("wind").Seed);

double v = wind.Sample(x / 25.0, y / 25.0); // cells 25 units wide
Soft light and dark blobs, all of about one size
One layer, cells 25 pixels wide. Black is −0.6 or less, the lightest gold 0.6 or more.

One layer alone looks soft and regular, blobs of about one size. For most terrain, use fractal noise.

GradientNoise is a small read-only struct, cheap to copy and to keep in a field. default(GradientNoise) is the same noise as new GradientNoise(0).

GradientNoise properties

Property Type Description
Seed ulong Read-only. The seed the noise was created with.
MaxCoordinate double Constant. 2^40, the largest x and y, either sign, that Sample takes.

GradientNoise methods

Method Description
GradientNoise(ulong seed) Creates the noise of the seed. Any value works, and different seeds give unrelated fields.
Sample(double x, double y) The noise at the point, from −1 to 1. 0 at integer points. Throws ArgumentOutOfRangeException for a coordinate that is NaN, infinite or beyond MaxCoordinate.

Fractal noise

FractalNoise adds up several layers of gradient noise, called octaves, each finer and fainter than the one before (fractal Brownian motion, or fBm). The first octave sets the large shapes and the others add detail to them. The sum is scaled to stay from −1 to 1.

// FractalSettings.Default: four octaves, features about 32 units across
var height = new FractalNoise(world.Fork("height").Seed);

double h = height.Sample(x, y); // x and y in world units
Four squares of the same noise: the first soft and blobby, each next one with the same large shapes and finer, rougher detail
1, 2, 4 and 6 octaves of one seed, at a frequency of 1/64. More octaves keep the large shapes and add finer detail.

Each octave has its own seed and is shifted by a fraction of a cell, so the octaves never share their integer points: there is no grid of zeros, as there would be with one noise stacked on itself at doubled frequencies. A sample costs one gradient sample per octave.

FractalNoise is immutable and thread-safe: build it once, when the world is made, and sample it from anywhere.

FractalNoise properties

Property Type Description
Seed ulong Read-only. The seed the noise was created with. Octave i is seeded with StableHash.Combine(Seed, i).
Settings FractalSettings Read-only. The settings the noise was created with.
MaxOctaves int Constant. 16, the most octaves a noise can have.

FractalNoise methods

Method Description
FractalNoise(ulong seed) Creates the noise of the seed with FractalSettings.Default.
FractalNoise(ulong seed, FractalSettings settings) Creates the noise of the seed with those settings. Throws ArgumentNullException for null settings, and ArgumentOutOfRangeException for settings out of range, see settings.
Sample(double x, double y) The noise at the point in world units, from −1 to 1. Throws ArgumentOutOfRangeException for a coordinate that is NaN or infinite, or that the highest octave would sample beyond GradientNoise.MaxCoordinate.

Settings

FractalSettings holds the four numbers that shape a fractal noise. It is a record of plain data, so it can sit in a settings file next to the world seed, and two settings with the same values are equal. Change it with with:

var settings = FractalSettings.Default with { Frequency = 1.0 / 100, Octaves = 6, Gain = 0.45 };
var mountains = new FractalNoise(world.Fork("mountains").Seed, settings);
  • Frequency sets the size of the large shapes: the cells per world unit of the first octave. 1/32 gives features about 32 units across; a higher frequency gives smaller ones.
  • Octaves sets how much detail there is. Each octave adds a finer layer and costs one more sample.
  • Lacunarity sets how much finer each octave is than the one before: 2 doubles the frequency each time.
  • Gain sets how much fainter each octave is than the one before, also called persistence: 0.5 halves it each time. Lower is smoother, higher is rougher.
Three squares of noise with the same detail, its shapes smaller from left to right
A frequency of 1/100, 1/50 and 1/25, with five octaves.
Three squares of noise with the same large shapes, smooth on the left and grainy on the right
A gain of 0.3, 0.5 and 0.7, with six octaves at a frequency of 1/64.

The FractalNoise constructor checks the settings, and throws ArgumentOutOfRangeException for a frequency, lacunarity or gain that isn’t a finite number above 0, or for octaves outside 1 to MaxOctaves.

FractalSettings properties

Property Type Description
Default FractalSettings Static. The settings below at their defaults, one shared instance.
Frequency double Init-only. The cells per world unit of the first octave. 1.0 / 32 by default: features about 32 units across.
Octaves int Init-only. The number of octaves, 1 to MaxOctaves. 4 by default.
Lacunarity double Init-only. The frequency of each octave over that of the one before. 2 by default.
Gain double Init-only. The amplitude of each octave over that of the one before. 0.5 by default.

The range in practice

Both noise types return values from −1 to 1, but they rarely come near the ends. Over 200 000 random points:

Noise Half of the values within 90 % within 98 % within
GradientNoise ±0.22 ±0.51 ±0.66
FractalNoise, 4 octaves (the default) ±0.13 ±0.31 ±0.42
FractalNoise, 6 octaves ±0.12 ±0.30 ±0.40

More octaves narrow the range, because independent layers partly cancel out. The values average about 0, so a test against 0 opens about half of the plane. Choose thresholds from the table: with the default settings, a test against 0.8 is almost never true. Where something needs the whole range, such as a color ramp, stretch the values, for example with Math.Clamp(v / 0.45, -1, 1).

Thresholds

A threshold turns noise into shapes. Caves are open where the noise is above a value: with the default frequency and five octaves, above 0.05 opens about 40 % of the cells, in winding pockets. Lower the threshold for more open space, raise it for less.

var caves = new FractalNoise(world.Fork("caves").Seed, FractalSettings.Default with { Octaves = 5 });

for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
    map[x, y] = caves.Sample(x, y) > 0.05 ? Tile.Floor : Tile.Rock;
On the left, noise over a square of cells; on the right, the same square with the cells above 0.05 in gold and the rest dark
The caves noise over 100 × 100 cells, and the cells above 0.05 as open floor.

Several bands of one noise give several kinds of ground, and a second noise with its own fork adds a second axis, such as moisture next to height:

var height = new FractalNoise(world.Fork("height").Seed);
var moisture = new FractalNoise(world.Fork("moisture").Seed);

var h = height.Sample(x, y);
var m = moisture.Sample(x, y);

var biome = h < -0.15 ? Biome.Water
          : h > 0.25 ? Biome.Mountain
          : m > 0.1 ? Biome.Forest
          : Biome.Grass;

Coordinates

GradientNoise.Sample takes any coordinates up to MaxCoordinate, 2^40 or about 1.1 × 10^12, either sign. Beyond it, a double keeps too few digits after the point for smooth noise, so Sample throws ArgumentOutOfRangeException instead of returning blocky values; it throws for NaN and infinities too.

FractalNoise.Sample multiplies the coordinates by each octave’s frequency, so the limit applies to the highest octave. With the default settings that octave has a frequency of 1/4, and world coordinates can go to about 4.4 × 10^12. Both limits are far beyond any game world, and integer cell coordinates work as they are.

The noise has no period: it never repeats, however far you go. Noise built on a permutation table repeats every 256 cells; this one hashes every cell from the seed instead.