- Home /
Set Random.seed for noise generator
Hello, I have downloaded a procedural terrain generator from this site and now I need to make it to generate a random terrain every time a new world is generated (since by default it always proceduraly generates the same looking world). I have found out, that it reqires changing Unity's Random.seed value that effects the noise generator. I have tried doing it manualy (typing in an int value) and it worked fine, but when I started using Random.range(-2147483648, 2147483647) the worlds started looking nothing like they are supposed to. I am not very good with C#, but I am pretty sure, that this is the one resposible for the seed value:
using UnityEngine;
using System.Collections;
public class NoiseModule{
protected const int B = 256;
protected int[] m_perm = new int[B + B];
public NoiseModule(int seed){
UnityEngine.Random.seed = seed;
int i, j, k;
for (i = 0; i < B; i++)
{
m_perm[i] = i;
}
while (--i != 0)
{
k = m_perm[i];
j = UnityEngine.Random.Range(0, B);
m_perm[i] = m_perm[j];
m_perm[j] = k;
}
for (i = 0; i < B; i++)
{
m_perm[B + i] = m_perm[i];
}
}
protected float Noise1D(float x)
{
//returns a noise value between -0.5 and 0.5
int ix0, ix1;
float fx0, fx1;
float s, n0, n1;
ix0 = (int)Mathf.Floor(x); // Integer part of x
fx0 = x - ix0; // Fractional part of x
fx1 = fx0 - 1.0f;
ix1 = (ix0 + 1) & 0xff;
ix0 = ix0 & 0xff; // Wrap to 0..255
s = NoiseUtil.FADE(fx0);
n0 = NoiseUtil.GRAD1(m_perm[ix0], fx0);
n1 = NoiseUtil.GRAD1(m_perm[ix1], fx1);
return 0.188f * NoiseUtil.LERP(s, n0, n1);
}
protected float Noise2D(float x, float y)
{
//returns a noise value between -0.75 and 0.75
int ix0, iy0, ix1, iy1;
float fx0, fy0, fx1, fy1, s, t, nx0, nx1, n0, n1;
ix0 = (int)Mathf.Floor(x); // Integer part of x
iy0 = (int)Mathf.Floor(y); // Integer part of y
fx0 = x - ix0; // Fractional part of x
fy0 = y - iy0; // Fractional part of y
fx1 = fx0 - 1.0f;
fy1 = fy0 - 1.0f;
ix1 = (ix0 + 1) & 0xff; // Wrap to 0..255
iy1 = (iy0 + 1) & 0xff;
ix0 = ix0 & 0xff;
iy0 = iy0 & 0xff;
t = NoiseUtil.FADE(fy0);
s = NoiseUtil.FADE(fx0);
nx0 = NoiseUtil.GRAD2(m_perm[ix0 + m_perm[iy0]], fx0, fy0);
nx1 = NoiseUtil.GRAD2(m_perm[ix0 + m_perm[iy1]], fx0, fy1);
n0 = NoiseUtil.LERP(t, nx0, nx1);
nx0 = NoiseUtil.GRAD2(m_perm[ix1 + m_perm[iy0]], fx1, fy0);
nx1 = NoiseUtil.GRAD2(m_perm[ix1 + m_perm[iy1]], fx1, fy1);
n1 = NoiseUtil.LERP(t, nx0, nx1);
return 0.507f * NoiseUtil.LERP(s, n0, n1);
}
protected float Noise3D(float x, float y, float z)
{
//returns a noise value between -1.5 and 1.5
int ix0, iy0, ix1, iy1, iz0, iz1;
float fx0, fy0, fz0, fx1, fy1, fz1;
float s, t, r;
float nxy0, nxy1, nx0, nx1, n0, n1;
ix0 = (int)Mathf.Floor(x); // Integer part of x
iy0 = (int)Mathf.Floor(y); // Integer part of y
iz0 = (int)Mathf.Floor(z); // Integer part of z
fx0 = x - ix0; // Fractional part of x
fy0 = y - iy0; // Fractional part of y
fz0 = z - iz0; // Fractional part of z
fx1 = fx0 - 1.0f;
fy1 = fy0 - 1.0f;
fz1 = fz0 - 1.0f;
ix1 = (ix0 + 1) & 0xff; // Wrap to 0..255
iy1 = (iy0 + 1) & 0xff;
iz1 = (iz0 + 1) & 0xff;
ix0 = ix0 & 0xff;
iy0 = iy0 & 0xff;
iz0 = iz0 & 0xff;
r = NoiseUtil.FADE(fz0);
t = NoiseUtil.FADE(fy0);
s = NoiseUtil.FADE(fx0);
nxy0 = NoiseUtil.GRAD3(m_perm[ix0 + m_perm[iy0 + m_perm[iz0]]], fx0, fy0, fz0);
nxy1 = NoiseUtil.GRAD3(m_perm[ix0 + m_perm[iy0 + m_perm[iz1]]], fx0, fy0, fz1);
nx0 = NoiseUtil.LERP(r, nxy0, nxy1);
nxy0 = NoiseUtil.GRAD3(m_perm[ix0 + m_perm[iy1 + m_perm[iz0]]], fx0, fy1, fz0);
nxy1 = NoiseUtil.GRAD3(m_perm[ix0 + m_perm[iy1 + m_perm[iz1]]], fx0, fy1, fz1);
nx1 = NoiseUtil.LERP(r, nxy0, nxy1);
n0 = NoiseUtil.LERP(t, nx0, nx1);
nxy0 = NoiseUtil.GRAD3(m_perm[ix1 + m_perm[iy0 + m_perm[iz0]]], fx1, fy0, fz0);
nxy1 = NoiseUtil.GRAD3(m_perm[ix1 + m_perm[iy0 + m_perm[iz1]]], fx1, fy0, fz1);
nx0 = NoiseUtil.LERP(r, nxy0, nxy1);
nxy0 = NoiseUtil.GRAD3(m_perm[ix1 + m_perm[iy1 + m_perm[iz0]]], fx1, fy1, fz0);
nxy1 = NoiseUtil.GRAD3(m_perm[ix1 + m_perm[iy1 + m_perm[iz1]]], fx1, fy1, fz1);
nx1 = NoiseUtil.LERP(r, nxy0, nxy1);
n1 = NoiseUtil.LERP(t, nx0, nx1);
return 0.936f * NoiseUtil.LERP(s, n0, n1);
}
public virtual float FractalNoise2D(float x, float y, int octNum, float frq, float amp)
{
return 1;
}
}
I am aware, that I am changing a seed value of a seed value generator and that might be the cause of all of this, that is why I treied storing the new random seed value before applying it to Unity engine as well as generating it from the system clock (found that on google), but non of that made difference. This is how the world looks like after generating it with seed value between -1000 and 1000:
Worlds look wrong, but I noticed they are random.
Answer by Daumas18 · Aug 05, 2015 at 07:25 AM
So what I ended up doing is accessing this C# script with one of my JS scripts (I had to put the C# in a "Plugins" folder for my JS to be able to access it) and assigning the value for "seed" from there. Appears that the only way to do this seed generation process properly is to change this exact value in this exact script.
Your answer
Follow this Question
Related Questions
How do I seed the random number generator myself and still get pseudorandom values? 2 Answers
Generate minecraft alike terrain 0 Answers
Questions about changes to UnityEngine.Random. Random.Seed deprecated 3 Answers
Randomly place a specific number of objects? 1 Answer
Random.Range affects the Random.seed 1 Answer