diff --git a/common/buildcraft/BuildCraftCore.java b/common/buildcraft/BuildCraftCore.java index 45463bae..d66c286b 100644 --- a/common/buildcraft/BuildCraftCore.java +++ b/common/buildcraft/BuildCraftCore.java @@ -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; diff --git a/common/buildcraft/core/BlockSpring.java b/common/buildcraft/core/BlockSpring.java index b0f83047..c56f6140 100644 --- a/common/buildcraft/core/BlockSpring.java +++ b/common/buildcraft/core/BlockSpring.java @@ -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 { diff --git a/common/buildcraft/core/lib/block/BlockBuildCraft.java b/common/buildcraft/core/lib/block/BlockBuildCraft.java index 51d3d837..8a663e18 100644 --- a/common/buildcraft/core/lib/block/BlockBuildCraft.java +++ b/common/buildcraft/core/lib/block/BlockBuildCraft.java @@ -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; diff --git a/common/buildcraft/core/lib/utils/Utils.java b/common/buildcraft/core/lib/utils/Utils.java index c28455d7..5a3ecc7d 100644 --- a/common/buildcraft/core/lib/utils/Utils.java +++ b/common/buildcraft/core/lib/utils/Utils.java @@ -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 directions = new ArrayList(Arrays.asList(ForgeDirection.VALID_DIRECTIONS)); /** diff --git a/common/buildcraft/core/lib/utils/XorShift128Random.java b/common/buildcraft/core/lib/utils/XorShift128Random.java new file mode 100644 index 00000000..6c42cb65 --- /dev/null +++ b/common/buildcraft/core/lib/utils/XorShift128Random.java @@ -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; + } +}