diff options
author | Muramasa <haydenkilloh@gmail.com> | 2016-09-24 23:05:18 +0100 |
---|---|---|
committer | Muramasa <haydenkilloh@gmail.com> | 2016-09-24 23:05:18 +0100 |
commit | fed8109b17fd2b41de17ceef92f37e39dc1ffc53 (patch) | |
tree | 9806dc7273bfc03fbb7a6eff1f6d0f98ce3a5bc3 /src/main/java/gregtech/api/objects | |
parent | 291839871e1233ba0c72e48cf16847246a1ccc7c (diff) | |
parent | 380211de8a2aaeaa6b24bbde34625bba4e626d79 (diff) | |
download | GT5-Unofficial-fed8109b17fd2b41de17ceef92f37e39dc1ffc53.tar.gz GT5-Unofficial-fed8109b17fd2b41de17ceef92f37e39dc1ffc53.tar.bz2 GT5-Unofficial-fed8109b17fd2b41de17ceef92f37e39dc1ffc53.zip |
Update with exp changes
Diffstat (limited to 'src/main/java/gregtech/api/objects')
10 files changed, 339 insertions, 76 deletions
diff --git a/src/main/java/gregtech/api/objects/ElementStack.java b/src/main/java/gregtech/api/objects/ElementStack.java index f3e1543614..773700f057 100644 --- a/src/main/java/gregtech/api/objects/ElementStack.java +++ b/src/main/java/gregtech/api/objects/ElementStack.java @@ -17,14 +17,14 @@ public class ElementStack implements Cloneable { @Override public ElementStack clone() { - return new ElementStack(mElement, mAmount); + try { return (ElementStack) super.clone(); } catch (Exception e) { return new ElementStack(mElement, mAmount); } } @Override public boolean equals(Object aObject) { if (aObject == this) return true; if (aObject == null) return false; - if (aObject instanceof Element) return aObject == mElement; + if (aObject instanceof Element) {return aObject == mElement;} if (aObject instanceof ElementStack) return ((ElementStack) aObject).mElement == mElement && (mAmount < 0 || ((ElementStack) aObject).mAmount < 0 || ((ElementStack) aObject).mAmount == mAmount); return false; diff --git a/src/main/java/gregtech/api/objects/GT_ArrayList.java b/src/main/java/gregtech/api/objects/GT_ArrayList.java index 5bc781e281..d16974bc78 100644 --- a/src/main/java/gregtech/api/objects/GT_ArrayList.java +++ b/src/main/java/gregtech/api/objects/GT_ArrayList.java @@ -6,6 +6,7 @@ import java.util.Collection; public class GT_ArrayList<E> extends ArrayList<E> { private static final long serialVersionUID = 1L; + private int size_sS; private final boolean mAllowNulls; @@ -17,13 +18,13 @@ public class GT_ArrayList<E> extends ArrayList<E> { public GT_ArrayList(boolean aAllowNulls, E... aArray) { super(Arrays.asList(aArray)); mAllowNulls = aAllowNulls; - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} } public GT_ArrayList(boolean aAllowNulls, Collection<? extends E> aList) { super(aList); mAllowNulls = aAllowNulls; - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} } @Override @@ -46,14 +47,14 @@ public class GT_ArrayList<E> extends ArrayList<E> { @Override public boolean addAll(Collection<? extends E> aList) { boolean rReturn = super.addAll(aList); - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} return rReturn; } @Override public boolean addAll(int aIndex, Collection<? extends E> aList) { boolean rReturn = super.addAll(aIndex, aList); - if (!mAllowNulls) for (int i = 0; i < size(); i++) if (get(i) == null) remove(i--); + if (!mAllowNulls) {size_sS=size(); for (int i = 0; i < size_sS; i++) if (get(i) == null) {remove(i--);size_sS=size();}} return rReturn; } }
\ No newline at end of file diff --git a/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java b/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java index caf46b3980..12c2e28a37 100644 --- a/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java +++ b/src/main/java/gregtech/api/objects/GT_CopiedBlockTexture.java @@ -36,7 +36,7 @@ public class GT_CopiedBlockTexture implements ITexture { this(aBlock, aSide, aMeta, Dyes._NULL.mRGBa); } - private IIcon getIcon(int aSide) { + private final IIcon getIcon(int aSide) { if (mSide == 6) return mBlock.getIcon(aSide, mMeta); return mBlock.getIcon(mSide, mMeta); } diff --git a/src/main/java/gregtech/api/objects/GT_FluidStack.java b/src/main/java/gregtech/api/objects/GT_FluidStack.java index 3f9e52818c..f4aad8ed5e 100644 --- a/src/main/java/gregtech/api/objects/GT_FluidStack.java +++ b/src/main/java/gregtech/api/objects/GT_FluidStack.java @@ -29,28 +29,25 @@ public class GT_FluidStack extends FluidStack { this(aFluid.getFluid(), aFluid.amount); } - public static synchronized void fixAllThoseFuckingFluidIDs() { - if (ForgeVersion.getBuildVersion() < 1355) { - while (lock) { - try { + public static final synchronized void fixAllThoseFuckingFluidIDs() { + if (ForgeVersion.getBuildVersion() < 1355 && ForgeVersion.getRevisionVersion() < 4) { + try { + while (lock) { Thread.sleep(1); - } catch (InterruptedException e) { - } - } + }} catch (InterruptedException e) {} lock = true; for (GT_FluidStack tFluid : sAllFluidStacks) tFluid.fixFluidIDForFucksSake(); - for (Map<Fluid, ?> tMap : GregTech_API.sFluidMappings) - try { + try { + for (Map<Fluid, ?> tMap : GregTech_API.sFluidMappings) GT_Utility.reMap(tMap); - } catch (Throwable e) { - e.printStackTrace(GT_Log.err); - } + } catch (Throwable e) {e.printStackTrace(GT_Log.err);} lock = false; } } - public void fixFluidIDForFucksSake() { - if (ForgeVersion.getBuildVersion() < 1355) { + @Deprecated + public final void fixFluidIDForFucksSake() { + if (ForgeVersion.getBuildVersion() < 1355 && ForgeVersion.getRevisionVersion() < 4) { int fluidID; try { fluidID = this.getFluid().getID(); @@ -67,7 +64,7 @@ public class GT_FluidStack extends FluidStack { @Override public FluidStack copy() { - if (ForgeVersion.getBuildVersion() < 1355) { + if (ForgeVersion.getBuildVersion() < 1355 && ForgeVersion.getRevisionVersion() < 4) { fixFluidIDForFucksSake(); } return new GT_FluidStack(this); diff --git a/src/main/java/gregtech/api/objects/GT_ItemStack.java b/src/main/java/gregtech/api/objects/GT_ItemStack.java index f865b348a0..9791cadeeb 100644 --- a/src/main/java/gregtech/api/objects/GT_ItemStack.java +++ b/src/main/java/gregtech/api/objects/GT_ItemStack.java @@ -24,16 +24,16 @@ public class GT_ItemStack { this(GT_Utility.intToStack(aHashCode)); } - public ItemStack toStack() { + public final ItemStack toStack() { if (mItem == null) return null; return new ItemStack(mItem, 1, mMetaData); } - public boolean isStackEqual(ItemStack aStack) { + public final boolean isStackEqual(ItemStack aStack) { return GT_Utility.areStacksEqual(toStack(), aStack); } - public boolean isStackEqual(GT_ItemStack aStack) { + public final boolean isStackEqual(GT_ItemStack aStack) { return GT_Utility.areStacksEqual(toStack(), aStack.toStack()); } diff --git a/src/main/java/gregtech/api/objects/GT_RenderedTexture.java b/src/main/java/gregtech/api/objects/GT_RenderedTexture.java index e1627da287..70b71cf125 100644 --- a/src/main/java/gregtech/api/objects/GT_RenderedTexture.java +++ b/src/main/java/gregtech/api/objects/GT_RenderedTexture.java @@ -70,10 +70,11 @@ public class GT_RenderedTexture implements ITexture, IColorModulationContainer { Tessellator.instance.setColorRGBA((int) (mRGBa[0] * 0.5F), (int) (mRGBa[1] * 0.5F), (int) (mRGBa[2] * 0.5F), mAllowAlpha ? 255 - mRGBa[3] : 255); IIcon aIcon = mIconContainer.getIcon(); - double d3 = aIcon.getInterpolatedU(aRenderer.renderMaxX * 16.0D); - double d4 = aIcon.getInterpolatedU(aRenderer.renderMinX * 16.0D); - double d5 = aIcon.getInterpolatedV(aRenderer.renderMinZ * 16.0D); - double d6 = aIcon.getInterpolatedV(aRenderer.renderMaxZ * 16.0D); + float d_16 = 16.0F; + float d3 = (float)aIcon.getInterpolatedU(aRenderer.renderMaxX * d_16); + float d4 = (float)aIcon.getInterpolatedU(aRenderer.renderMinX * d_16); + float d5 = (float)aIcon.getInterpolatedV(aRenderer.renderMinZ * d_16); + float d6 = (float)aIcon.getInterpolatedV(aRenderer.renderMaxZ * d_16); if (aRenderer.renderMinX < 0.0D || aRenderer.renderMaxX > 1.0D) { d3 = aIcon.getMaxU(); @@ -85,24 +86,24 @@ public class GT_RenderedTexture implements ITexture, IColorModulationContainer { d6 = aIcon.getMaxV(); } - double d11 = aX + aRenderer.renderMinX; - double d12 = aX + aRenderer.renderMaxX; - double d13 = aY + aRenderer.renderMinY; - double d14 = aZ + aRenderer.renderMinZ; - double d15 = aZ + aRenderer.renderMaxZ; + float d11 = aX + (float)aRenderer.renderMinX; + float d12 = aX + (float)aRenderer.renderMaxX; + float d13 = aY + (float)aRenderer.renderMinY; + float d14 = aZ + (float)aRenderer.renderMinZ; + float d15 = aZ + (float)aRenderer.renderMaxZ; - Tessellator.instance.addVertexWithUV(d11, d13, d15, d3, d6); - Tessellator.instance.addVertexWithUV(d11, d13, d14, d3, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d14, d4, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d15, d4, d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d15, (double)d3, (double)d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d14, (double)d3, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d14, (double)d4, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d15, (double)d4, (double)d6); if ((aIcon = mIconContainer.getOverlayIcon()) != null) { Tessellator.instance.setColorRGBA(128, 128, 128, 255); - Tessellator.instance.addVertexWithUV(d11, d13, d15, d3, d6); - Tessellator.instance.addVertexWithUV(d11, d13, d14, d3, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d14, d4, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d15, d4, d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d15, (double)d3, (double)d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d14, (double)d3, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d14, (double)d4, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d15, (double)d4, (double)d6); } } diff --git a/src/main/java/gregtech/api/objects/GT_SidedTexture.java b/src/main/java/gregtech/api/objects/GT_SidedTexture.java index 2d27ecbc25..2b4f144393 100644 --- a/src/main/java/gregtech/api/objects/GT_SidedTexture.java +++ b/src/main/java/gregtech/api/objects/GT_SidedTexture.java @@ -78,10 +78,11 @@ public class GT_SidedTexture implements ITexture, IColorModulationContainer { Tessellator.instance.setColorRGBA((int) (mRGBa[0] * 0.5F), (int) (mRGBa[1] * 0.5F), (int) (mRGBa[2] * 0.5F), mAllowAlpha ? 255 - mRGBa[3] : 255); IIcon aIcon = mIconContainer[0].getIcon(); - double d3 = aIcon.getInterpolatedU(aRenderer.renderMaxX * 16.0D); - double d4 = aIcon.getInterpolatedU(aRenderer.renderMinX * 16.0D); - double d5 = aIcon.getInterpolatedV(aRenderer.renderMinZ * 16.0D); - double d6 = aIcon.getInterpolatedV(aRenderer.renderMaxZ * 16.0D); + float d_16 = 16.0F; + float d3 = (float)aIcon.getInterpolatedU(aRenderer.renderMaxX * d_16); + float d4 = (float)aIcon.getInterpolatedU(aRenderer.renderMinX * d_16); + float d5 = (float)aIcon.getInterpolatedV(aRenderer.renderMinZ * d_16); + float d6 = (float)aIcon.getInterpolatedV(aRenderer.renderMaxZ * d_16); if (aRenderer.renderMinX < 0.0D || aRenderer.renderMaxX > 1.0D) { d3 = aIcon.getMaxU(); @@ -93,24 +94,24 @@ public class GT_SidedTexture implements ITexture, IColorModulationContainer { d6 = aIcon.getMaxV(); } - double d11 = aX + aRenderer.renderMinX; - double d12 = aX + aRenderer.renderMaxX; - double d13 = aY + aRenderer.renderMinY; - double d14 = aZ + aRenderer.renderMinZ; - double d15 = aZ + aRenderer.renderMaxZ; + float d11 = aX + (float)aRenderer.renderMinX; + float d12 = aX + (float)aRenderer.renderMaxX; + float d13 = aY + (float)aRenderer.renderMinY; + float d14 = aZ + (float)aRenderer.renderMinZ; + float d15 = aZ + (float)aRenderer.renderMaxZ; - Tessellator.instance.addVertexWithUV(d11, d13, d15, d3, d6); - Tessellator.instance.addVertexWithUV(d11, d13, d14, d3, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d14, d4, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d15, d4, d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d15, (double)d3, (double)d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d14, (double)d3, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d14, (double)d4, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d15, (double)d4, (double)d6); if ((aIcon = mIconContainer[0].getOverlayIcon()) != null) { Tessellator.instance.setColorRGBA(128, 128, 128, 255); - d3 = aIcon.getInterpolatedU(aRenderer.renderMaxX * 16.0D); - d4 = aIcon.getInterpolatedU(aRenderer.renderMinX * 16.0D); - d5 = aIcon.getInterpolatedV(aRenderer.renderMinZ * 16.0D); - d6 = aIcon.getInterpolatedV(aRenderer.renderMaxZ * 16.0D); + d3 = (float)aIcon.getInterpolatedU(aRenderer.renderMaxX * d_16); + d4 = (float)aIcon.getInterpolatedU(aRenderer.renderMinX * d_16); + d5 = (float)aIcon.getInterpolatedV(aRenderer.renderMinZ * d_16); + d6 = (float)aIcon.getInterpolatedV(aRenderer.renderMaxZ * d_16); if (aRenderer.renderMinX < 0.0D || aRenderer.renderMaxX > 1.0D) { d3 = aIcon.getMaxU(); @@ -122,16 +123,16 @@ public class GT_SidedTexture implements ITexture, IColorModulationContainer { d6 = aIcon.getMaxV(); } - d11 = aX + aRenderer.renderMinX; - d12 = aX + aRenderer.renderMaxX; - d13 = aY + aRenderer.renderMinY; - d14 = aZ + aRenderer.renderMinZ; - d15 = aZ + aRenderer.renderMaxZ; + d11 = aX + (float)aRenderer.renderMinX; + d12 = aX + (float)aRenderer.renderMaxX; + d13 = aY + (float)aRenderer.renderMinY; + d14 = aZ + (float)aRenderer.renderMinZ; + d15 = aZ + (float)aRenderer.renderMaxZ; - Tessellator.instance.addVertexWithUV(d11, d13, d15, d3, d6); - Tessellator.instance.addVertexWithUV(d11, d13, d14, d3, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d14, d4, d5); - Tessellator.instance.addVertexWithUV(d12, d13, d15, d4, d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d15, (double)d3, (double)d6); + Tessellator.instance.addVertexWithUV((double)d11, (double)d13, (double)d14, (double)d3, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d14, (double)d4, (double)d5); + Tessellator.instance.addVertexWithUV((double)d12, (double)d13, (double)d15, (double)d4, (double)d6); } } diff --git a/src/main/java/gregtech/api/objects/ItemData.java b/src/main/java/gregtech/api/objects/ItemData.java index cb741344ba..8a47867afb 100644 --- a/src/main/java/gregtech/api/objects/ItemData.java +++ b/src/main/java/gregtech/api/objects/ItemData.java @@ -93,32 +93,32 @@ public class ItemData { mByProducts = rList.toArray(new MaterialStack[rList.size()]); } - public boolean hasValidPrefixMaterialData() { + public final boolean hasValidPrefixMaterialData() { return mPrefix != null && mMaterial != null && mMaterial.mMaterial != null; } - public boolean hasValidPrefixData() { + public final boolean hasValidPrefixData() { return mPrefix != null; } - public boolean hasValidMaterialData() { + public final boolean hasValidMaterialData() { return mMaterial != null && mMaterial.mMaterial != null; } - public ArrayList<MaterialStack> getAllMaterialStacks() { + public final ArrayList<MaterialStack> getAllMaterialStacks() { ArrayList<MaterialStack> rList = new ArrayList(); if (hasValidMaterialData()) rList.add(mMaterial); rList.addAll(Arrays.asList(mByProducts)); return rList; } - public MaterialStack getByProduct(int aIndex) { + public final MaterialStack getByProduct(int aIndex) { return aIndex >= 0 && aIndex < mByProducts.length ? mByProducts[aIndex] : null; } @Override public String toString() { if (mPrefix == null || mMaterial == null || mMaterial.mMaterial == null) return ""; - return mPrefix.name() + mMaterial.mMaterial.mName; + return String.valueOf(new StringBuilder().append(mPrefix.name()).append(mMaterial.mMaterial.mName())); } }
\ No newline at end of file diff --git a/src/main/java/gregtech/api/objects/MaterialStack.java b/src/main/java/gregtech/api/objects/MaterialStack.java index 5ddb88423a..6066d3cf23 100644 --- a/src/main/java/gregtech/api/objects/MaterialStack.java +++ b/src/main/java/gregtech/api/objects/MaterialStack.java @@ -17,7 +17,7 @@ public class MaterialStack implements Cloneable { @Override public MaterialStack clone() { - return new MaterialStack(mMaterial, mAmount); + try { return (MaterialStack) super.clone(); } catch (Exception e) { return new MaterialStack(mMaterial, mAmount); } } @Override @@ -32,7 +32,15 @@ public class MaterialStack implements Cloneable { @Override public String toString() { - return (mMaterial.mMaterialList.size() > 1 && mAmount > 1 ? "(" : "") + mMaterial.getToolTip(true) + (mMaterial.mMaterialList.size() > 1 && mAmount > 1 ? ")" : "") + (mAmount > 1 ? mAmount : ""); + String temp1 = "", temp2 = mMaterial.getToolTip(true), temp3 = "", temp4 = ""; + if (mAmount > 1) { + temp4 = String.valueOf(mAmount); + if (mMaterial.mMaterialList.size() > 1) { + temp1 = "("; + temp3 = ")"; + } + } + return String.valueOf(new StringBuilder().append(temp1).append(temp2).append(temp3).append(temp4)); } @Override diff --git a/src/main/java/gregtech/api/objects/XSTR.java b/src/main/java/gregtech/api/objects/XSTR.java new file mode 100644 index 0000000000..2f185ac3bb --- /dev/null +++ b/src/main/java/gregtech/api/objects/XSTR.java @@ -0,0 +1,255 @@ +package gregtech.api.objects; +/** + * A subclass of java.util.random that implements the Xorshift random number + * generator + * + * - it is 30% faster than the generator from Java's library - it produces + * random sequences of higher quality than java.util.Random - this class also + * provides a clone() function + * + * Usage: XSRandom rand = new XSRandom(); //Instantiation x = rand.nextInt(); + * //pull a random number + * + * To use the class in legacy code, you may also instantiate an XSRandom object + * and assign it to a java.util.Random object: java.util.Random rand = new + * XSRandom(); + * + * for an explanation of the algorithm, see + * http://demesos.blogspot.com/2011/09/pseudo-random-number-generators.html + * + * @author Wilfried Elmenreich University of Klagenfurt/Lakeside Labs + * http://www.elmenreich.tk + * + * This code is released under the GNU Lesser General Public License Version 3 + * http://www.gnu.org/licenses/lgpl-3.0.txt + */ + +import java.util.Random; +import java.util.concurrent.atomic.AtomicLong; + +/** + * XSTR - Xorshift ThermiteRandom + * Modified by Bogdan-G + * 03.06.2016 + * version 0.0.4 + */ +public class XSTR extends Random { + + private static final long serialVersionUID = 6208727693524452904L; + private long seed; + private long last; + private static final long GAMMA = 0x9e3779b97f4a7c15L; + private static final int PROBE_INCREMENT = 0x9e3779b9; + private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL; + private static final double DOUBLE_UNIT = 0x1.0p-53; // 1.0 / (1L << 53) + private static final float FLOAT_UNIT = 0x1.0p-24f; // 1.0f / (1 << 24) + + /* + MODIFIED BY: Robotia + Modification: Implemented Random class seed generator + */ + /** + * Creates a new pseudo random number generator. The seed is initialized to + * the current time, as if by + * <code>setSeed(System.currentTimeMillis());</code>. + */ + public XSTR() { + this(seedUniquifier() ^ System.nanoTime()); + } + private static final AtomicLong seedUniquifier + = new AtomicLong(8682522807148012L); + + private static long seedUniquifier() { + // L'Ecuyer, "Tables of Linear Congruential Generators of + // Different Sizes and Good Lattice Structure", 1999 + for (;;) { + long current = seedUniquifier.get(); + long next = current * 181783497276652981L; + if (seedUniquifier.compareAndSet(current, next)) { + return next; + } + } + } + + /** + * Creates a new pseudo random number generator, starting with the specified + * seed, using <code>setSeed(seed);</code>. + * + * @param seed the initial seed + */ + public XSTR(long seed) { + this.seed = seed; + } + public boolean nextBoolean() { + return next(1) != 0; + } + + public double nextDouble() { + return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT; + } + /** + * Returns the current state of the seed, can be used to clone the object + * + * @return the current seed + */ + public synchronized long getSeed() { + return seed; + } + + /** + * Sets the seed for this pseudo random number generator. As described + * above, two instances of the same random class, starting with the same + * seed, produce the same results, if the same methods are called. + * + * @param seed the new seed + */ + public synchronized void setSeed(long seed) { + this.seed = seed; + } + + /** + * @return Returns an XSRandom object with the same state as the original + */ + @Override + public XSTR clone() { + return new XSTR(getSeed()); + } + + /** + * Implementation of George Marsaglia's elegant Xorshift random generator + * 30% faster and better quality than the built-in java.util.random see also + * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml + * + * @param nbits + * @return + */ + public int next(int nbits) { + long x = seed; + x ^= (x << 21); + x ^= (x >>> 35); + x ^= (x << 4); + seed = x; + x &= ((1L << nbits) - 1); + return (int) x; + } + boolean haveNextNextGaussian = false; + double nextNextGaussian = 0; + synchronized public double nextGaussian() { + // See Knuth, ACP, Section 3.4.1 Algorithm C. + if (haveNextNextGaussian) { + haveNextNextGaussian = false; + return nextNextGaussian; + } else { + double v1, v2, s; + do { + v1 = 2 * nextDouble() - 1; // between -1 and 1 + v2 = 2 * nextDouble() - 1; // between -1 and 1 + s = v1 * v1 + v2 * v2; + } while (s >= 1 || s == 0); + double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); + nextNextGaussian = v2 * multiplier; + haveNextNextGaussian = true; + return v1 * multiplier; + } + } + /** + * Returns a pseudorandom, uniformly distributed {@code int} value between 0 + * (inclusive) and the specified value (exclusive), drawn from this random + * number generator's sequence. The general contract of {@code nextInt} is + * that one {@code int} value in the specified range is pseudorandomly + * generated and returned. All {@code bound} possible {@code int} values are + * produced with (approximately) equal probability. The method + * {@code nextInt(int bound)} is implemented by class {@code Random} as if + * by: + * <pre> {@code + * public int nextInt(int bound) { + * if (bound <= 0) + * throw new IllegalArgumentException("bound must be positive"); + * + * if ((bound & -bound) == bound) // i.e., bound is a power of 2 + * return (int)((bound * (long)next(31)) >> 31); + * + * int bits, val; + * do { + * bits = next(31); + * val = bits % bound; + * } while (bits - val + (bound-1) < 0); + * return val; + * }}</pre> + * + * <p>The hedge "approx + * imately" is used in the foregoing description only because the next + * method is only approximately an unbiased source of independently chosen + * bits. If it were a perfect source of randomly chosen bits, then the + * algorithm shown would choose {@code int} values from the stated range + * with perfect uniformity. + * <p> + * The algorithm is slightly tricky. It rejects values that would result in + * an uneven distribution (due to the fact that 2^31 is not divisible by n). + * The probability of a value being rejected depends on n. The worst case is + * n=2^30+1, for which the probability of a reject is 1/2, and the expected + * number of iterations before the loop terminates is 2. + * <p> + * The algorithm treats the case where n is a power of two specially: it + * returns the correct number of high-order bits from the underlying + * pseudo-random number generator. In the absence of special treatment, the + * correct number of <i>low-order</i> bits would be returned. Linear + * congruential pseudo-random number generators such as the one implemented + * by this class are known to have short periods in the sequence of values + * of their low-order bits. Thus, this special case greatly increases the + * length of the sequence of values returned by successive calls to this + * method if n is a small power of two. + * + * @param bound the upper bound (exclusive). Must be positive. + * @return the next pseudorandom, uniformly distributed {@code int} value + * between zero (inclusive) and {@code bound} (exclusive) from this random + * number generator's sequence + * @throws IllegalArgumentException if bound is not positive + * @since 1.2 + */ + public int nextInt(int bound) { + //if (bound <= 0) { + //throw new RuntimeException("BadBound"); + //} + + /*int r = next(31); + int m = bound - 1; + if ((bound & m) == 0) // i.e., bound is a power of 2 + { + r = (int) ((bound * (long) r) >> 31); + } else { + for (int u = r; + u - (r = u % bound) + m < 0; + u = next(31)) + ; + } + return r;*/ + //speedup, new nextInt ~+40% + last = seed ^ (seed << 21); + last ^= (last >>> 35); + last ^= (last << 4); + seed = last; + int out = (int) last % bound; + return (out < 0) ? -out : out; + } + public int nextInt() { + return next(32); + } + + public float nextFloat() { + return next(24) * FLOAT_UNIT; + } + + public long nextLong() { + // it's okay that the bottom word remains signed. + return ((long)(next(32)) << 32) + next(32); + } + + public void nextBytes(byte[] bytes_arr) { + for (int iba = 0, lenba = bytes_arr.length; iba < lenba; ) + for (int rndba = nextInt(), + nba = Math.min(lenba - iba, Integer.SIZE/Byte.SIZE); + nba-- > 0; rndba >>= Byte.SIZE) + bytes_arr[iba++] = (byte)rndba; + } +}
\ No newline at end of file |