mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-09 18:14:06 +09:00
com.torvald → net.torvald
Former-commit-id: 375604da8a20a6ba7cd0a8d05a44add02b2d04f4 Former-commit-id: 287287c5920b07618174d7a7573f049d350ded66
This commit is contained in:
13
src/net/torvald/random/Fudge3.kt
Normal file
13
src/net/torvald/random/Fudge3.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package net.torvald.random
|
||||
|
||||
import java.util.Random
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-02-03.
|
||||
*/
|
||||
class Fudge3
|
||||
/**
|
||||
* Define new set of fudge dice with three dice.
|
||||
* @param randfunc java.util.Random or its extension
|
||||
*/
|
||||
(randfunc: Random) : FudgeDice(randfunc, 3)
|
||||
47
src/net/torvald/random/FudgeDice.kt
Normal file
47
src/net/torvald/random/FudgeDice.kt
Normal file
@@ -0,0 +1,47 @@
|
||||
package net.torvald.random
|
||||
|
||||
import java.util.Random
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-02-03.
|
||||
*/
|
||||
open class FudgeDice
|
||||
/**
|
||||
* Define new set of fudge dice with given counts.
|
||||
* @param randfunc java.util.Random or its extension
|
||||
* *
|
||||
* @param counts amount of die
|
||||
*/
|
||||
(private val randfunc: Random, val diceCounts: Int) {
|
||||
|
||||
/**
|
||||
* Roll dice and get result.
|
||||
* @return Normal distributed integer [-N , N] for diceCount of N. 0 is the most frequent return.
|
||||
*/
|
||||
fun roll(): Int {
|
||||
var diceResult = 0
|
||||
for (c in 0..diceCounts - 1) {
|
||||
diceResult += rollSingleDie()
|
||||
}
|
||||
|
||||
return diceResult
|
||||
}
|
||||
|
||||
/**
|
||||
* Roll dice and get result, for array index
|
||||
* @return Normal distributed integer [0 , N] for N = 2 × DiceCounts + 1. 0 is the most frequent return.
|
||||
*/
|
||||
fun rollForArray(): Int {
|
||||
return roll() + diceCounts
|
||||
}
|
||||
|
||||
val sizeOfProbabilityRange: Int
|
||||
get() = 2 * diceCounts + 1
|
||||
|
||||
/**
|
||||
* @return integer randomly picked from {-1, 0, 1}
|
||||
*/
|
||||
private fun rollSingleDie(): Int {
|
||||
return randfunc.nextInt(3) - 1
|
||||
}
|
||||
}
|
||||
59
src/net/torvald/random/HQRNG.java
Normal file
59
src/net/torvald/random/HQRNG.java
Normal file
@@ -0,0 +1,59 @@
|
||||
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 <a href="http://www.amazon.com/gp/product/0521880688?ie=UTF8&tag=javamex-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0521880688">Numerical Recipes: The Art of Scientific Computing</a>,
|
||||
* 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 <b>not</b> thread-safe. In order to make it thread-safe, uncomment the lock-related lines. It is also <b>not</b> cryptographically secure, like the java.security.SecureRandom class.
|
||||
* @author Numerical Recipes
|
||||
*/
|
||||
|
||||
public class HQRNG extends Random {
|
||||
|
||||
//private Lock l = new ReentrantLock();
|
||||
private long u;
|
||||
private long v = 4101842887655102017L;
|
||||
private long w = 1;
|
||||
|
||||
public HQRNG() {
|
||||
this(System.nanoTime());
|
||||
}
|
||||
public HQRNG(long seed) {
|
||||
//l.lock();
|
||||
u = seed ^ v;
|
||||
nextLong();
|
||||
v = u;
|
||||
nextLong();
|
||||
w = v;
|
||||
nextLong();
|
||||
//l.unlock();
|
||||
}
|
||||
|
||||
public long nextLong() {
|
||||
// l.lock();
|
||||
try {
|
||||
u = u * 2862933555777941757L + 7046029254386353087L;
|
||||
v ^= v >>> 17;
|
||||
v ^= v << 31;
|
||||
v ^= v >>> 8;
|
||||
w = 4294957665L * (w & 0xffffffff) + (w >>> 32);
|
||||
long x = u ^ (u << 21);
|
||||
x ^= x >>> 35;
|
||||
x ^= x << 4;
|
||||
long ret = (x + v) ^ w;
|
||||
return ret;
|
||||
} finally {
|
||||
//l.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
protected int next(int bits) {
|
||||
return (int) (nextLong() >>> (64-bits));
|
||||
}
|
||||
|
||||
}
|
||||
1440
src/net/torvald/random/MTRandom.java
Normal file
1440
src/net/torvald/random/MTRandom.java
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user