Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Java Perlin Noise seed addition

1. ## Java Perlin Noise seed addition

I got a working perlin noise 1D code and it always outputs the same result. I want to add a seed to it, that it only gives the same when using the same seed, but I don't know where I should put it.

Here's the class with the Perlin noise:

```public class ClassNoise
{
public static List<Integer[]> primes = new ArrayList<Integer[]>();
static
{
int prime1 = 0;
int prime2 = 0;
int prime3 = 0;
for (int i = 1; i < 10000000; i+=2)
{

if (isPrime(i))
{
prime1 = i;
for (; i < 10000000; i+=2)
{
if (isPrime(i))
{
prime2 = i;
for (; i < 10000000; i+=2)
{
if (isPrime(i))
{
prime3 = i;

}
}
}
}
}
}
}

public static float Noise(int x, int octave)
{
x = (x << 13) ^ x;
return (float) (1.0 - ((x
* (x * x * primes.get(octave)[0] + primes.get(octave)[1]) + primes.get(octave)[2]) & Integer.MAX_VALUE) / 1073741824f);
}

public static float PerlinNoise1D(float x, float persistence, int octaves)
{
float total = 0;
float p = persistence;
int n = octaves - 1;

float[] octavePostMutliply = { 1, 1, 1, 1, 1, 1, 1, 1 };

for (int i = 0; i <= n; i++)
{

float frequency = (float) Math.pow(2, i);
double amplitude = Math.pow(p, octaves - i);
total += InterpolatedNoise(x * frequency, i) * amplitude
* octavePostMutliply[i];
}

}

private static float InterpolatedNoise(float x, int octave)
{
int integer_X = (int) x;
float fractional_X = x - integer_X;

float v1 = SmoothNoise1D(integer_X, octave);
float v2 = SmoothNoise1D(integer_X + 1, octave);

return CosineInterpolate(v1, v2, fractional_X);

}

public static float CosineInterpolate(float a, float b, float x)
{
float ft = (float) (x * Math.PI);
float f = (float) ((1 - Math.cos(ft)) * 0.5);

return a * (1 - f) + b * f;
}

public static float SmoothNoise1D(int x, int octave)
{
return Noise(x, octave) / 2 + Noise(x - 1, octave) / 4 + Noise(x + 1, octave) / 4;
}

private static boolean isPrime(int n)
{
if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
}```

2. ## Re: Java Perlin Noise seed addition

I'm not sure what Perlin is, but if a seed is permitted, it would usually be fed to a constructor, similar to how the Random() constructor can be fed with a long value as a seed. Check the Perlin API to see if there is a constructor that allows a seed.