Skip to content

Instantly share code, notes, and snippets.

@NelsonMinar
Last active October 10, 2021 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NelsonMinar/81405e870267c4641d758fb2bc33d655 to your computer and use it in GitHub Desktop.
Save NelsonMinar/81405e870267c4641d758fb2bc33d655 to your computer and use it in GitHub Desktop.
Histograms of noise functions
"""Print out some histograms for the outputs of various noise functions.
The noise function is sampled with a uniformly random set of x/y coordinates in the range [ -100_000, 100_000 ]
"""
from opensimplex import OpenSimplex
import noise
import random, math
buckets = 20
samples = 1000000
char_width = 50
def do(noisefunc):
histogram = [0] * (buckets)
for i in range(samples):
x = random.uniform(-100000, 100000)
y = random.uniform(-100000, 100000)
v = noisefunc(x, y)
p = (v+1) / 2
b = round(p * (buckets-1))
histogram[b] += 1
biggest = max(histogram)
for i in range(buckets):
v = histogram[i]
n = ((i+0.5) / buckets) * 2 - 1
w = char_width * v / biggest
c = ''.join(['*'] * round(w))
print(f'{n: .2f} {c}')
print("Perlin noise")
do(noise.pnoise2)
print("Simplex noise")
do(noise.snoise2)
print("OpenSimplex noise2d")
osn = OpenSimplex()
do(osn.noise2d)
Perlin noise
-0.95
-0.85
-0.75
-0.65
-0.55 ***
-0.45 ***********
-0.35 *****************
-0.25 *********************************
-0.15 *******************************************
-0.05 *************************************************
0.05 **************************************************
0.15 ********************************************
0.25 **********************************
0.35 ****************
0.45 ***********
0.55 ***
0.65 *
0.75
0.85
0.95
Simplex noise
-0.95
-0.85 *********
-0.75 **************
-0.65 *************************
-0.55 ************************************************
-0.45 *************************************************
-0.35 **********************************************
-0.25 **********************************************
-0.15 ***********************************************
-0.05 **********************************************
0.05 ************************************************
0.15 **************************************************
0.25 ************************************************
0.35 ************************************************
0.45 **************************************************
0.55 *************************************************
0.65 ************************
0.75 **************
0.85 *********
0.95
OpenSimplex noise2d
-0.95
-0.85
-0.75 *****
-0.65 ***************
-0.55 **************************
-0.45 *****************************************
-0.35 **************************************************
-0.25 **************************************************
-0.15 *************************************************
-0.05 *************************************************
0.05 **************************************************
0.15 *************************************************
0.25 *************************************************
0.35 *************************************************
0.45 ****************************************
0.55 **************************
0.65 ***************
0.75 *****
0.85
0.95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment