replace randomness algorith with Xorshift128+, both more efficient and better quality

This commit is contained in:
asiekierka 2015-05-05 09:51:42 +02:00
parent 22f8511b6a
commit 3f01e45b67
5 changed files with 49 additions and 4 deletions

View file

@ -95,6 +95,7 @@ import buildcraft.core.lib.engines.TileEngineBase;
import buildcraft.core.lib.network.ChannelHandler;
import buildcraft.core.lib.utils.ColorUtils;
import buildcraft.core.lib.utils.NBTUtils;
import buildcraft.core.lib.utils.XorShift128Random;
import buildcraft.core.network.PacketHandlerCore;
import buildcraft.core.properties.WorldPropertyIsDirt;
import buildcraft.core.properties.WorldPropertyIsFarmland;
@ -142,7 +143,7 @@ public class BuildCraftCore extends BuildCraftMod {
Full, NoDynamic
}
public static Random random = new Random();
public static XorShift128Random random = new XorShift128Random();
public static RenderMode render = RenderMode.Full;
public static boolean debugWorldgen = false;
public static boolean modifyWorld = false;

View file

@ -20,10 +20,11 @@ import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import buildcraft.core.lib.utils.XorShift128Random;
public class BlockSpring extends Block {
public static final Random rand = new Random();
public static final XorShift128Random rand = new XorShift128Random();
public enum EnumSpring {

View file

@ -32,6 +32,7 @@ import buildcraft.api.tiles.IHasWork;
import buildcraft.core.BCCreativeTab;
import buildcraft.core.lib.utils.ResourceUtils;
import buildcraft.core.lib.utils.Utils;
import buildcraft.core.lib.utils.XorShift128Random;
public abstract class BlockBuildCraft extends BlockContainer {
protected static boolean keepInventory = false;
@ -45,7 +46,7 @@ public abstract class BlockBuildCraft extends BlockContainer {
@SideOnly(Side.CLIENT)
public IIcon[][] icons;
protected final Random rand = new Random();
protected final XorShift128Random rand = new XorShift128Random();
protected int renderPass;
protected int maxPasses = 1;

View file

@ -50,7 +50,7 @@ import buildcraft.core.proxy.CoreProxy;
public final class Utils {
public static final Random RANDOM = new Random();
public static final XorShift128Random RANDOM = new XorShift128Random();
private static final List<ForgeDirection> directions = new ArrayList<ForgeDirection>(Arrays.asList(ForgeDirection.VALID_DIRECTIONS));
/**

View file

@ -0,0 +1,42 @@
package buildcraft.core.lib.utils;
import java.util.Random;
/**
* Based on http://xorshift.di.unimi.it/xorshift128plus.c
*/
public class XorShift128Random {
private static final Random seed = new Random();
private static final double DOUBLE_UNIT = 0x1.0p-53;
private long[] s = new long[2];
public XorShift128Random() {
s[0] = seed.nextLong();
s[1] = seed.nextLong();
}
public long nextLong() {
long s1 = s[0];
long s0 = s[1];
s[0] = s0;
s1 ^= (s1 << 23);
s[1] = (s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26)) + s0;
return s[1];
}
public int nextInt() {
return (int) (nextLong() & 0xFFFFFFFF);
}
public boolean nextBoolean() {
return (nextLong() & 0x1) != 0;
}
public int nextInt(int size) {
return (int) (nextLong() % size);
}
public double nextDouble() {
return (double) (long) (nextLong() & 0x1FFFFFFFFFFFFFL) * DOUBLE_UNIT;
}
}