Compare commits
3 Commits
71c0d508f7
...
4681ce8a13
Author | SHA1 | Date | |
---|---|---|---|
4681ce8a13 | |||
5c42f4eba1 | |||
7b85c90140 |
@ -4,7 +4,7 @@ kotlin.code.style=official
|
|||||||
specifyKotlinAsDependency=false
|
specifyKotlinAsDependency=false
|
||||||
|
|
||||||
projectGroup=ru.dbotthepony.kommons
|
projectGroup=ru.dbotthepony.kommons
|
||||||
projectVersion=3.4.0
|
projectVersion=3.5.1
|
||||||
|
|
||||||
guavaDepVersion=33.0.0
|
guavaDepVersion=33.0.0
|
||||||
gsonDepVersion=2.8.9
|
gsonDepVersion=2.8.9
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
package ru.dbotthepony.kommons.random
|
||||||
|
|
||||||
|
import java.util.random.RandomGenerator
|
||||||
|
|
||||||
|
/**
|
||||||
|
* From [gjrand website](https://gjrand.sourceforge.net):
|
||||||
|
*
|
||||||
|
* The gjrand project is about pseudo-random number generation for the purpose of simulations,
|
||||||
|
* Monte-Carlo integration, computer games and the like. It is intended that statistical
|
||||||
|
* properties be extremely good for such purposes. However, gjrand is not suitable as a
|
||||||
|
* high security random number generator for cryptography, the state lottery draw, etc.
|
||||||
|
*/
|
||||||
|
open class GJRAND64Random protected constructor(
|
||||||
|
@JvmField
|
||||||
|
protected var s0: Long,
|
||||||
|
@JvmField
|
||||||
|
protected var s1: Long,
|
||||||
|
@JvmField
|
||||||
|
protected var s2: Long,
|
||||||
|
@JvmField
|
||||||
|
protected var s3: Long,
|
||||||
|
marker: Nothing?
|
||||||
|
) : RandomGenerator {
|
||||||
|
constructor(seed0: Long, seed1: Long) : this(seed0, seed1, SEED2_INIT, SEED3_INIT, null)
|
||||||
|
constructor(seed: Long) : this(seed, 0L)
|
||||||
|
|
||||||
|
final override fun nextLong(): Long {
|
||||||
|
s1 += s2
|
||||||
|
s0 = s0.rotateLeft(32)
|
||||||
|
s2 = s2 xor s1
|
||||||
|
s3 += 0x55aa96a5L
|
||||||
|
s0 += s1
|
||||||
|
s2 = s2.rotateLeft(23)
|
||||||
|
s1 = s1 xor s0
|
||||||
|
s0 += s2
|
||||||
|
s1 = s1.rotateLeft(19)
|
||||||
|
s2 += s0
|
||||||
|
s1 += s3
|
||||||
|
return s0
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val SEED2_INIT = 2000001L
|
||||||
|
const val SEED3_INIT = 0L
|
||||||
|
}
|
||||||
|
}
|
@ -28,7 +28,7 @@ open class JSF32Random protected constructor(
|
|||||||
* see [https://burtleburtle.net/bob/rand/smallprng.html](https://burtleburtle.net/bob/rand/smallprng.html) and
|
* see [https://burtleburtle.net/bob/rand/smallprng.html](https://burtleburtle.net/bob/rand/smallprng.html) and
|
||||||
* [https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html](https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html)
|
* [https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html](https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html)
|
||||||
*/
|
*/
|
||||||
constructor(seed: Int) : this(0xf1ea5eed.toInt(), seed, seed, seed, null) {
|
constructor(seed: Int) : this(SEED0_INIT, seed, seed, seed, null) {
|
||||||
for (i in 0 until 20)
|
for (i in 0 until 20)
|
||||||
nextInt()
|
nextInt()
|
||||||
}
|
}
|
||||||
@ -49,10 +49,6 @@ open class JSF32Random protected constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
const val SEED0_INIT = 0xf1ea5eed.toInt()
|
||||||
@Deprecated("JSF suffers from 'weak seed' weakness, using raw initialization is highly discouraged, unless serializing/deserializing")
|
|
||||||
fun raw(s0: Int, s1: Int, s2: Int, s3: Int): JSF32Random {
|
|
||||||
return JSF32Random(s0, s1, s2, s3, null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ open class JSF64Random(
|
|||||||
* see [https://burtleburtle.net/bob/rand/smallprng.html](https://burtleburtle.net/bob/rand/smallprng.html) and
|
* see [https://burtleburtle.net/bob/rand/smallprng.html](https://burtleburtle.net/bob/rand/smallprng.html) and
|
||||||
* [https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html](https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html)
|
* [https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html](https://www.pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html)
|
||||||
*/
|
*/
|
||||||
constructor(seed: Long) : this(0xf1ea5eedL, seed, seed, seed, null) {
|
constructor(seed: Long) : this(SEED0_INIT, seed, seed, seed, null) {
|
||||||
for (i in 0 until 20)
|
for (i in 0 until 20)
|
||||||
nextLong()
|
nextLong()
|
||||||
}
|
}
|
||||||
@ -48,10 +48,6 @@ open class JSF64Random(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
const val SEED0_INIT = 0xf1ea5eedL
|
||||||
@Deprecated("JSF suffers from 'weak seed' weakness, using raw initialization is highly discouraged, unless serializing/deserializing")
|
|
||||||
fun raw(s0: Long, s1: Long, s2: Long, s3: Long): JSF64Random {
|
|
||||||
return JSF64Random(s0, s1, s2, s3, null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,12 +50,4 @@ open class WOB2MRandom protected constructor(
|
|||||||
seed0 = temp - --count
|
seed0 = temp - --count
|
||||||
return seed1
|
return seed1
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
@JvmStatic
|
|
||||||
@Deprecated("Quality of manual seeding is unknown (and probably very, very bad!), so don't use this unless serializing/deserializing")
|
|
||||||
fun raw(seed0: Long, seed1: Long, counter: Long): WOB2MRandom {
|
|
||||||
return WOB2MRandom(seed0, seed1, counter, null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user