Archived
0

Seed based random generator

This commit is contained in:
Forwolk
2018-07-27 13:29:31 +03:00
parent 7d48095430
commit b2f5af9a84
5 changed files with 94 additions and 2 deletions

View File

@@ -0,0 +1,23 @@
package mc.world.generated_world;
public final class SeedRandomGenerator {
public static double random (int x, int y, int seed) {
x = Math.abs(x - y) + 1;
y = Math.abs(y - x) + 1;
for (int i = 0; i < 40; i ++) {
int a1 = x % 13;
int a2 = x % 31;
int a3 = x % 89;
int a4 = y % 359;
int a5 = y % 7;
int a6 = y % 313;
int a7 = y % 8461;
int a8 = y % 105467;
int a9 = x % 105943;
y = x + seed;
x += a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
}
return ((x + y) % 100000) / 100000d;
}
}

View File

@@ -0,0 +1,58 @@
package mc.world.generated_world;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import static org.junit.Assert.*;
public class SeedRandomGeneratorTest {
@Test
public void randomTest() throws Exception {
double maxDiff = 0;
double maxDisp = 0;
for (int i = 0; i < 100; i ++) {
double mid = 0;
double disp = 0;
int seed = (int) (Math.random() * Integer.MAX_VALUE);
for (int x = -1000; x < 1000; x++) {
for (int y = -1000; y < 1000; y++) {
double rnd = SeedRandomGenerator.random(x, y, seed);
mid += rnd;
disp += (rnd - 0.5) * (rnd - 0.5);
}
}
mid = mid/4000000;
disp = Math.sqrt(disp)/4000000;
if (maxDiff < Math.abs(mid - 0.5)) {
maxDiff = Math.abs(mid - 0.5);
}
if (maxDisp < disp) {
maxDisp = disp;
}
System.out.printf("Iteration %d.\t mid: %.3f, \tdisp %.6f\n", i + 1, mid, disp);
}
System.out.printf("Max diff: %.3f\n", maxDiff);
System.out.printf("Max disp: %.6f\n", maxDisp);
assertTrue(maxDiff > 0);
}
@Test
public void generateImage () throws Exception {
int h = 500;
int w = 500;
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
int seed = (int) (Math.random() * Integer.MAX_VALUE) / 1024;
for (int x = 0; x < w; x ++) {
for (int y = 0; y < h; y ++) {
image.setRGB(x, y, (int) (0xffffff * SeedRandomGenerator.random(x, y, seed)));
}
}
ImageIO.write(image, "bmp", new File("out", "seed_random.png"));
}
}