From d7576ce8448a45d61341c25e801146f3291f8c14 Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Mon, 20 Feb 2017 01:41:01 +0900 Subject: [PATCH] New RNG for the game and Joise Former-commit-id: 4a5b7f7ef6546d04be106d881e7d3f9dd6dc1b57 Former-commit-id: 90c15fa64cde39e33206ff7be645db9bceda1161 --- .../sudoplay/joise/generator/CMWC4096.java | 91 ------------------- src/com/sudoplay/joise/generator/KISS.java | 81 ----------------- src/com/sudoplay/joise/generator/LCG.java | 72 --------------- src/com/sudoplay/joise/generator/MWC.java | 75 --------------- src/com/sudoplay/joise/generator/MWC256.java | 83 ----------------- .../sudoplay/joise/generator/XORShift.java | 82 ----------------- .../joise/generator/Xorshift128plus.kt | 31 +++++++ .../joise/module/ModuleAutoCorrect.java | 4 +- .../joise/module/ModuleBasisFunction.java | 6 +- src/net/torvald/random/HQRNG.kt | 56 +++--------- 10 files changed, 50 insertions(+), 531 deletions(-) delete mode 100755 src/com/sudoplay/joise/generator/CMWC4096.java delete mode 100755 src/com/sudoplay/joise/generator/KISS.java delete mode 100755 src/com/sudoplay/joise/generator/LCG.java delete mode 100755 src/com/sudoplay/joise/generator/MWC.java delete mode 100755 src/com/sudoplay/joise/generator/MWC256.java delete mode 100755 src/com/sudoplay/joise/generator/XORShift.java create mode 100644 src/com/sudoplay/joise/generator/Xorshift128plus.kt diff --git a/src/com/sudoplay/joise/generator/CMWC4096.java b/src/com/sudoplay/joise/generator/CMWC4096.java deleted file mode 100755 index 42c6b3f04..000000000 --- a/src/com/sudoplay/joise/generator/CMWC4096.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2013 Jason Taylor. - * Released as open-source under the Apache License, Version 2.0. - * - * ============================================================================ - * | Joise - * ============================================================================ - * - * Copyright (C) 2013 Jason Taylor - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============================================================================ - * | Accidental Noise Library - * | -------------------------------------------------------------------------- - * | Joise is a derivative work based on Josua Tippetts' C++ library: - * | http://accidentalnoise.sourceforge.net/index.html - * ============================================================================ - * - * Copyright (C) 2011 Joshua Tippetts - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -package com.sudoplay.joise.generator; - -/** - * Complimentary multiply with carry. - */ -public class CMWC4096 extends BasePRNG { - - protected int c; - protected int[] Q = new int[4096]; - protected LCG lcg = new LCG(); - protected int i; - - public CMWC4096() { - setSeed(10000); - } - - @Override - public int get() { - long t; - long a = 18782L; - long b = 4294967295L; - int r = (int) (b - 1); - i = (i + 1) & 4095; - t = a * Q[i] + c; - c = (int) (t >> 32); - t = (t & b) + c; - if (t > r) { - c++; - t = t - b; - } - return Q[i] = (int) (r - t); - } - - @Override - public void setSeed(long seed) { - lcg.setSeed(seed); - for (int i = 0; i < 4096; i++) { - Q[i] = lcg.get(); - } - c = lcg.getTarget(18781); - } - -} diff --git a/src/com/sudoplay/joise/generator/KISS.java b/src/com/sudoplay/joise/generator/KISS.java deleted file mode 100755 index 71236dc2b..000000000 --- a/src/com/sudoplay/joise/generator/KISS.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2013 Jason Taylor. - * Released as open-source under the Apache License, Version 2.0. - * - * ============================================================================ - * | Joise - * ============================================================================ - * - * Copyright (C) 2013 Jason Taylor - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============================================================================ - * | Accidental Noise Library - * | -------------------------------------------------------------------------- - * | Joise is a derivative work based on Josua Tippetts' C++ library: - * | http://accidentalnoise.sourceforge.net/index.html - * ============================================================================ - * - * Copyright (C) 2011 Joshua Tippetts - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -package com.sudoplay.joise.generator; - -public class KISS extends BasePRNG { - - protected LCG lcg = new LCG(); - protected int z, w, jsr, jcong; - - public KISS() { - setSeed(10000); - } - - @Override - public int get() { - z = 36969 * (z & 65535) + (z >> 16); - w = 18000 * (w & 65535) + (w >> 16); - int mwc = (z << 16) + w; - jcong = 69069 * jcong + 1234567; - jsr ^= (jsr << 17); - jsr ^= (jsr >> 13); - jsr ^= (jsr << 5); - return ((mwc ^ jcong) + jsr); - } - - @Override - public void setSeed(long seed) { - lcg.setSeed(seed); - z = lcg.get(); - w = lcg.get(); - jsr = lcg.get(); - jcong = lcg.get(); - } - -} diff --git a/src/com/sudoplay/joise/generator/LCG.java b/src/com/sudoplay/joise/generator/LCG.java deleted file mode 100755 index 5d4f887bc..000000000 --- a/src/com/sudoplay/joise/generator/LCG.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2013 Jason Taylor. - * Released as open-source under the Apache License, Version 2.0. - * - * ============================================================================ - * | Joise - * ============================================================================ - * - * Copyright (C) 2013 Jason Taylor - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============================================================================ - * | Accidental Noise Library - * | -------------------------------------------------------------------------- - * | Joise is a derivative work based on Josua Tippetts' C++ library: - * | http://accidentalnoise.sourceforge.net/index.html - * ============================================================================ - * - * Copyright (C) 2011 Joshua Tippetts - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -package com.sudoplay.joise.generator; - -/** - * Linear congruential generator. - */ -public class LCG extends BasePRNG { - - protected long state; - - public LCG() { - setSeed(10000); - } - - @Override - public int get() { - return (int) (state = (25214903917L * state + 11) % 281474976710656L); - } - - @Override - public void setSeed(long seed) { - state = (int) seed; - } - -} diff --git a/src/com/sudoplay/joise/generator/MWC.java b/src/com/sudoplay/joise/generator/MWC.java deleted file mode 100755 index 1e8c0a24b..000000000 --- a/src/com/sudoplay/joise/generator/MWC.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2013 Jason Taylor. - * Released as open-source under the Apache License, Version 2.0. - * - * ============================================================================ - * | Joise - * ============================================================================ - * - * Copyright (C) 2013 Jason Taylor - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============================================================================ - * | Accidental Noise Library - * | -------------------------------------------------------------------------- - * | Joise is a derivative work based on Josua Tippetts' C++ library: - * | http://accidentalnoise.sourceforge.net/index.html - * ============================================================================ - * - * Copyright (C) 2011 Joshua Tippetts - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -package com.sudoplay.joise.generator; - -/** - * Multiply with carry. - */ -public class MWC extends BasePRNG { - - protected LCG lcg = new LCG(); - protected long x; - protected long a = 0xffffda61L; - - public MWC() { - setSeed(10000); - } - - @Override - public int get() { - return (int) (x = (a * (x & 0xffffffffL)) + (x >>> 32)); - } - - @Override - public void setSeed(long seed) { - lcg.setSeed(seed); - x = lcg.get() & 0xffffffffL; - } - -} diff --git a/src/com/sudoplay/joise/generator/MWC256.java b/src/com/sudoplay/joise/generator/MWC256.java deleted file mode 100755 index cd22c4113..000000000 --- a/src/com/sudoplay/joise/generator/MWC256.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2013 Jason Taylor. - * Released as open-source under the Apache License, Version 2.0. - * - * ============================================================================ - * | Joise - * ============================================================================ - * - * Copyright (C) 2013 Jason Taylor - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============================================================================ - * | Accidental Noise Library - * | -------------------------------------------------------------------------- - * | Joise is a derivative work based on Josua Tippetts' C++ library: - * | http://accidentalnoise.sourceforge.net/index.html - * ============================================================================ - * - * Copyright (C) 2011 Joshua Tippetts - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -package com.sudoplay.joise.generator; - -/** - * Multiply with carry. - */ -public class MWC256 extends BasePRNG { - - protected int[] Q = new int[256]; - protected int c; - protected LCG lcg = new LCG(); - protected byte b = (byte) 255; - - public MWC256() { - setSeed(10000); - } - - @Override - public int get() { - long t; - long a = 809430660L; - t = a * Q[++b & 0xFF] + c; - c = (int) (t >> 32); - return Q[b & 0xFF] = (int) t; - } - - @Override - public void setSeed(long seed) { - lcg.setSeed(seed); - for (int i = 0; i < 256; i++) { - Q[i] = lcg.get(); - } - c = lcg.getTarget(809430660); - } - -} diff --git a/src/com/sudoplay/joise/generator/XORShift.java b/src/com/sudoplay/joise/generator/XORShift.java deleted file mode 100755 index 1f4c3723b..000000000 --- a/src/com/sudoplay/joise/generator/XORShift.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2013 Jason Taylor. - * Released as open-source under the Apache License, Version 2.0. - * - * ============================================================================ - * | Joise - * ============================================================================ - * - * Copyright (C) 2013 Jason Taylor - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============================================================================ - * | Accidental Noise Library - * | -------------------------------------------------------------------------- - * | Joise is a derivative work based on Josua Tippetts' C++ library: - * | http://accidentalnoise.sourceforge.net/index.html - * ============================================================================ - * - * Copyright (C) 2011 Joshua Tippetts - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -package com.sudoplay.joise.generator; - -public class XORShift extends BasePRNG { - - protected int x, y, z, w, v; - protected LCG lcg = new LCG(); - - public XORShift() { - setSeed(10000); - } - - @Override - public int get() { - int t; - t = (x ^ (x >> 7)); - x = y; - y = z; - z = w; - w = v; - v = (v ^ (v << 6)) ^ (t ^ (t << 13)); - return (y + y + 1) * v; - } - - @Override - public void setSeed(long seed) { - lcg.setSeed(seed); - x = lcg.get(); - y = lcg.get(); - z = lcg.get(); - w = lcg.get(); - v = lcg.get(); - } - -} diff --git a/src/com/sudoplay/joise/generator/Xorshift128plus.kt b/src/com/sudoplay/joise/generator/Xorshift128plus.kt new file mode 100644 index 000000000..0ee83fd9b --- /dev/null +++ b/src/com/sudoplay/joise/generator/Xorshift128plus.kt @@ -0,0 +1,31 @@ +package com.sudoplay.joise.generator + +/** + * Created by SKYHi14 on 2017-02-20. + */ +class Xorshift128plus : BasePRNG() { + + private var s0: Long = 0 + private var s1: Long = 0 + + init { + setSeed(10000L) + } + + override fun get(): Int { + var x = s0 + val y = s1 + s0 = y + x = x xor (x shl 23) + s1 = x xor y xor (x ushr 17) xor (y ushr 26) + return (s1 + y).toInt() + } + + override fun setSeed(seed: Long) { + if (seed == 0L) + throw IllegalArgumentException("Invalid seed: cannot be zero") + + s0 = (6364136223846793005L * seed + 1442695040888963407L) + s1 = (6364136223846793005L * s0 + 1442695040888963407L) + } +} \ No newline at end of file diff --git a/src/com/sudoplay/joise/module/ModuleAutoCorrect.java b/src/com/sudoplay/joise/module/ModuleAutoCorrect.java index 29dc9f516..72cf68d3b 100755 --- a/src/com/sudoplay/joise/module/ModuleAutoCorrect.java +++ b/src/com/sudoplay/joise/module/ModuleAutoCorrect.java @@ -53,7 +53,7 @@ import static com.sudoplay.joise.noise.Util.clamp; import com.sudoplay.joise.ModuleInstanceMap; import com.sudoplay.joise.ModuleMap; import com.sudoplay.joise.ModulePropertyMap; -import com.sudoplay.joise.generator.LCG; +import com.sudoplay.joise.generator.Xorshift128plus; import com.sudoplay.util.Checked; public class ModuleAutoCorrect extends SourcedModule { @@ -125,7 +125,7 @@ public class ModuleAutoCorrect extends SourcedModule { if (!source.isModule() || locked) return; double mn, mx; - LCG lcg = new LCG(); + Xorshift128plus lcg = new Xorshift128plus(); // Calculate 2D mn = 10000.0; diff --git a/src/com/sudoplay/joise/module/ModuleBasisFunction.java b/src/com/sudoplay/joise/module/ModuleBasisFunction.java index 0f27bb3a9..bdd93f434 100755 --- a/src/com/sudoplay/joise/module/ModuleBasisFunction.java +++ b/src/com/sudoplay/joise/module/ModuleBasisFunction.java @@ -51,7 +51,7 @@ package com.sudoplay.joise.module; import com.sudoplay.joise.ModuleInstanceMap; import com.sudoplay.joise.ModuleMap; import com.sudoplay.joise.ModulePropertyMap; -import com.sudoplay.joise.generator.LCG; +import com.sudoplay.joise.generator.Xorshift128plus; import com.sudoplay.joise.noise.Interpolator; import com.sudoplay.joise.noise.Noise; @@ -192,8 +192,8 @@ public class ModuleBasisFunction extends SeedableModule { @Override public void setSeed(long seed) { super.setSeed(seed); - - LCG lcg = new LCG(); + + Xorshift128plus lcg = new Xorshift128plus(); lcg.setSeed(seed); double ax, ay, az; diff --git a/src/net/torvald/random/HQRNG.kt b/src/net/torvald/random/HQRNG.kt index e981694b3..141faa06c 100644 --- a/src/net/torvald/random/HQRNG.kt +++ b/src/net/torvald/random/HQRNG.kt @@ -2,56 +2,28 @@ package net.torvald.random import java.util.Random -//import java.util.concurrent.locks.*; - /** - * This class implements a better random number generator than the standard LCG that is implemented in java.util.Random. - * It is based on [Numerical Recipes: The Art of Scientific Computing](http://www.amazon.com/gp/product/0521880688?ie=UTF8&tag=javamex-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0521880688), - * and gives a good compromise between quality and speed. It is a combined generator: two XORShift generators are combined with an LCG and a multiply with carry generator. - * (Without going into all the details here, notice the two blocks of three shifts each, which are the XORShifts; the first line which is the LCG, similar to the standard - * Java Random algorithm, and the line between the two XORShifts, which is a multiply with carry generator.) - * Note that this version is **not** thread-safe. In order to make it thread-safe, uncomment the lock-related lines. It is also **not** cryptographically secure, like the java.security.SecureRandom class. - * @author Numerical Recipes + * Xorshift128+ */ - class HQRNG @JvmOverloads constructor(seed: Long = System.nanoTime()) : Random() { - //private Lock l = new ReentrantLock(); - private var u: Long = 0 - private var v = 4101842887655102017L - private var w: Long = 1 + private var s0: Long + private var s1: Long init { - //l.lock(); - u = seed xor v - nextLong() - v = u - nextLong() - w = v - nextLong() - //l.unlock(); + if (seed == 0L) + throw IllegalArgumentException("Invalid seed: cannot be zero") + + s0 = (6364136223846793005L * seed + 1442695040888963407L) + s1 = (6364136223846793005L * s0 + 1442695040888963407L) } override fun nextLong(): Long { - // l.lock(); - try { - u = u * 2862933555777941757L + 7046029254386353087L - v = v xor v.ushr(17) - v = v xor v.shl(31) - v = v xor v.ushr(8) - w = 4294957665L * w.and(0xffffffffL) + w.ushr(32) - var x = u xor u.shl(21) - x = x xor x.ushr(35) - x = x xor x.shl(4) - return x + v xor w - } - finally { - //l.unlock(); - } + var x = s0 + val y = s1 + s0 = y + x = x xor (x shl 23) + s1 = x xor y xor (x ushr 17) xor (y ushr 26) + return s1 + y } - - override fun next(bits: Int): Int { - return nextLong().ushr(64 - bits).toInt() - } - } \ No newline at end of file