Compare commits
3 Commits
862c248450
...
6272e753e2
Author | SHA1 | Date | |
---|---|---|---|
6272e753e2 | |||
d273586a3d | |||
09d662a815 |
@ -33,6 +33,7 @@ val jupiterVersion: String by project
|
|||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
|
maxHeapSize = "8G"
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -4,7 +4,7 @@ kotlin.code.style=official
|
|||||||
specifyKotlinAsDependency=false
|
specifyKotlinAsDependency=false
|
||||||
|
|
||||||
projectGroup=ru.dbotthepony.kommons
|
projectGroup=ru.dbotthepony.kommons
|
||||||
projectVersion=3.7.0
|
projectVersion=3.8.0
|
||||||
|
|
||||||
guavaDepVersion=33.0.0
|
guavaDepVersion=33.0.0
|
||||||
gsonDepVersion=2.8.9
|
gsonDepVersion=2.8.9
|
||||||
|
@ -23,7 +23,7 @@ open class GJRAND64Random protected constructor(
|
|||||||
) : RandomGenerator {
|
) : RandomGenerator {
|
||||||
constructor(seed0: Long, seed1: Long) : this(seed0, seed1, SEED2_INIT, SEED3_INIT, null) {
|
constructor(seed0: Long, seed1: Long) : this(seed0, seed1, SEED2_INIT, SEED3_INIT, null) {
|
||||||
for (i in 0 until 14)
|
for (i in 0 until 14)
|
||||||
nextLong()
|
round()
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(seed: Long) : this(seed, 0L)
|
constructor(seed: Long) : this(seed, 0L)
|
||||||
@ -39,10 +39,10 @@ open class GJRAND64Random protected constructor(
|
|||||||
s3 = SEED3_INIT
|
s3 = SEED3_INIT
|
||||||
|
|
||||||
for (i in 0 until 14)
|
for (i in 0 until 14)
|
||||||
nextLong()
|
round()
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun nextLong(): Long {
|
protected fun round(): Long {
|
||||||
s1 += s2
|
s1 += s2
|
||||||
s0 = s0.rotateLeft(32)
|
s0 = s0.rotateLeft(32)
|
||||||
s2 = s2 xor s1
|
s2 = s2 xor s1
|
||||||
@ -57,6 +57,10 @@ open class GJRAND64Random protected constructor(
|
|||||||
return s0
|
return s0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextLong(): Long {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val SEED2_INIT = 2000001L
|
const val SEED2_INIT = 2000001L
|
||||||
const val SEED3_INIT = 0L
|
const val SEED3_INIT = 0L
|
||||||
|
@ -29,11 +29,20 @@ open class JSF32Random protected constructor(
|
|||||||
* [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(SEED0_INIT, seed, seed, seed, null) {
|
constructor(seed: Int) : this(SEED0_INIT, seed, seed, seed, null) {
|
||||||
for (i in 0 until 20)
|
reinitialize(seed)
|
||||||
nextInt()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun nextInt(): Int {
|
protected fun reinitialize(seed: Int) {
|
||||||
|
s0 = SEED0_INIT
|
||||||
|
s1 = seed
|
||||||
|
s2 = seed
|
||||||
|
s3 = seed
|
||||||
|
|
||||||
|
for (i in 0 until 20)
|
||||||
|
round()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun round(): Int {
|
||||||
val e = s0 - s1.rotateLeft(27)
|
val e = s0 - s1.rotateLeft(27)
|
||||||
s0 = s1 xor s2.rotateLeft(17)
|
s0 = s1 xor s2.rotateLeft(17)
|
||||||
s1 = s2 + s3
|
s1 = s2 + s3
|
||||||
@ -42,6 +51,10 @@ open class JSF32Random protected constructor(
|
|||||||
return s3
|
return s3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextInt(): Int {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
|
|
||||||
override fun nextLong(): Long {
|
override fun nextLong(): Long {
|
||||||
val a = nextInt().toLong()
|
val a = nextInt().toLong()
|
||||||
val b = nextInt().toLong() and 0xFFFFFFFFL
|
val b = nextInt().toLong() and 0xFFFFFFFFL
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.kommons.random
|
package ru.dbotthepony.kommons.random
|
||||||
|
|
||||||
|
import ru.dbotthepony.kommons.random.JSF32Random.Companion
|
||||||
import java.util.random.RandomGenerator
|
import java.util.random.RandomGenerator
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,11 +35,20 @@ open class JSF64Random(
|
|||||||
* [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(SEED0_INIT, seed, seed, seed, null) {
|
constructor(seed: Long) : this(SEED0_INIT, seed, seed, seed, null) {
|
||||||
for (i in 0 until 20)
|
reinitialize(seed)
|
||||||
nextLong()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun nextLong(): Long {
|
protected fun reinitialize(seed: Long) {
|
||||||
|
s0 = SEED0_INIT
|
||||||
|
s1 = seed
|
||||||
|
s2 = seed
|
||||||
|
s3 = seed
|
||||||
|
|
||||||
|
for (i in 0 until 20)
|
||||||
|
round()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun round(): Long {
|
||||||
val e = s0 - s1.rotateLeft(7)
|
val e = s0 - s1.rotateLeft(7)
|
||||||
s0 = s1 xor s2.rotateLeft(13)
|
s0 = s1 xor s2.rotateLeft(13)
|
||||||
s1 = s2 + s3.rotateLeft(37)
|
s1 = s2 + s3.rotateLeft(37)
|
||||||
@ -47,6 +57,10 @@ open class JSF64Random(
|
|||||||
return s3
|
return s3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextLong(): Long {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val SEED0_INIT = 0xf1ea5eedL
|
const val SEED0_INIT = 0xf1ea5eedL
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,11 @@ open class LCG64Random(
|
|||||||
@JvmField
|
@JvmField
|
||||||
protected var seed: Long
|
protected var seed: Long
|
||||||
) : RandomGenerator {
|
) : RandomGenerator {
|
||||||
|
protected fun round(): Int {
|
||||||
|
this.seed = MULTIPLIER * this.seed + INCREMENT
|
||||||
|
return this.seed.ushr(32).toInt()
|
||||||
|
}
|
||||||
|
|
||||||
override fun nextLong(): Long {
|
override fun nextLong(): Long {
|
||||||
val a = nextInt().toLong()
|
val a = nextInt().toLong()
|
||||||
val b = nextInt().toLong() and 0xFFFFFFFFL
|
val b = nextInt().toLong() and 0xFFFFFFFFL
|
||||||
@ -22,8 +27,7 @@ open class LCG64Random(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun nextInt(): Int {
|
override fun nextInt(): Int {
|
||||||
this.seed = MULTIPLIER * this.seed + INCREMENT
|
return round()
|
||||||
return this.seed.ushr(32).toInt()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -10,7 +10,7 @@ open class PCG32Random(
|
|||||||
@JvmField
|
@JvmField
|
||||||
protected var seed: Long
|
protected var seed: Long
|
||||||
) : RandomGenerator {
|
) : RandomGenerator {
|
||||||
final override fun nextInt(): Int {
|
protected fun round(): Int {
|
||||||
var x = this.seed
|
var x = this.seed
|
||||||
this.seed = LCG64Random.MULTIPLIER * this.seed + LCG64Random.INCREMENT
|
this.seed = LCG64Random.MULTIPLIER * this.seed + LCG64Random.INCREMENT
|
||||||
|
|
||||||
@ -19,6 +19,10 @@ open class PCG32Random(
|
|||||||
return x.ushr(27).toInt().rotateLeft(rot.toInt())
|
return x.ushr(27).toInt().rotateLeft(rot.toInt())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextInt(): Int {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
|
|
||||||
override fun nextLong(): Long {
|
override fun nextLong(): Long {
|
||||||
val a = nextInt().toLong()
|
val a = nextInt().toLong()
|
||||||
val b = nextInt().toLong() and 0xFFFFFFFFL
|
val b = nextInt().toLong() and 0xFFFFFFFFL
|
||||||
|
@ -27,7 +27,23 @@ open class ShishuaRandom() : RandomGenerator {
|
|||||||
engine.initialize(longArrayOf(rng.nextLong(), rng.nextLong(), rng.nextLong(), rng.nextLong()))
|
engine.initialize(longArrayOf(rng.nextLong(), rng.nextLong(), rng.nextLong(), rng.nextLong()))
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun nextLong(): Long {
|
protected fun reinitialize(seed: Long) {
|
||||||
|
val rng = PCG32Random(seed)
|
||||||
|
engine.initialize(longArrayOf(rng.nextLong(), rng.nextLong(), rng.nextLong(), rng.nextLong()))
|
||||||
|
index = 16
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun reinitialize(seed0: Long, seed1: Long, seed2: Long, seed3: Long) {
|
||||||
|
engine.initialize(longArrayOf(seed0, seed1, seed2, seed3))
|
||||||
|
index = 16
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun reinitialize(seed: LongArray) {
|
||||||
|
engine.initialize(seed)
|
||||||
|
index = 16
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun round(): Long {
|
||||||
if (index == 16) {
|
if (index == 16) {
|
||||||
index = 0
|
index = 0
|
||||||
engine.step(acceptor, 1)
|
engine.step(acceptor, 1)
|
||||||
@ -35,4 +51,8 @@ open class ShishuaRandom() : RandomGenerator {
|
|||||||
|
|
||||||
return values[index++]
|
return values[index++]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextLong(): Long {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,20 +34,28 @@ open class WOB2MRandom protected constructor(
|
|||||||
) : RandomGenerator {
|
) : RandomGenerator {
|
||||||
constructor(seed0: Long, seed1: Long) : this(seed0, seed1, Long.MIN_VALUE + 10, null) {
|
constructor(seed0: Long, seed1: Long) : this(seed0, seed1, Long.MIN_VALUE + 10, null) {
|
||||||
for (i in 0 until 10)
|
for (i in 0 until 10)
|
||||||
nextLong()
|
round()
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun nextLong(): Long {
|
protected fun round(): Long {
|
||||||
val temp = seed0 + count++
|
val temp = seed0 + count++
|
||||||
seed0 = seed1 + temp.rotateLeft(12)
|
seed0 = seed1 + temp.rotateLeft(12)
|
||||||
seed1 = (0x0581af43eb71d8b3 * temp) xor seed0.rotateLeft(28)
|
seed1 = (0x0581af43eb71d8b3 * temp) xor seed0.rotateLeft(28)
|
||||||
return seed1
|
return seed1
|
||||||
}
|
}
|
||||||
|
|
||||||
fun previousLong(): Long {
|
override fun nextLong(): Long {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun previousRound(): Long {
|
||||||
val temp = 0x6cc3621b095c967b * (seed1 xor seed0.rotateLeft(28))
|
val temp = 0x6cc3621b095c967b * (seed1 xor seed0.rotateLeft(28))
|
||||||
seed1 = seed0 - temp.rotateLeft(12)
|
seed1 = seed0 - temp.rotateLeft(12)
|
||||||
seed0 = temp - --count
|
seed0 = temp - --count
|
||||||
return seed1
|
return seed1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open fun previousLong(): Long {
|
||||||
|
return previousRound()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ open class Xoshiro256PlusPlusRandom protected constructor(
|
|||||||
constructor(s0: Long, s1: Long, s2: Long, s3: Long) : this(s0, s1, s2, s3, null) {
|
constructor(s0: Long, s1: Long, s2: Long, s3: Long) : this(s0, s1, s2, s3, null) {
|
||||||
// discard some values so generator can get going if provided seed was "weak"
|
// discard some values so generator can get going if provided seed was "weak"
|
||||||
for (i in 0 until 32)
|
for (i in 0 until 32)
|
||||||
nextLong()
|
round()
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(seed: Long) : this(1L, 0L, 0L, 0L, null) {
|
constructor(seed: Long) : this(1L, 0L, 0L, 0L, null) {
|
||||||
@ -41,7 +41,7 @@ open class Xoshiro256PlusPlusRandom protected constructor(
|
|||||||
s3 = rng.nextLong()
|
s3 = rng.nextLong()
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun nextLong(): Long {
|
protected fun round(): Long {
|
||||||
val result = (s0 + s3).rotateLeft(23) + s1
|
val result = (s0 + s3).rotateLeft(23) + s1
|
||||||
val t = s1.shl(17)
|
val t = s1.shl(17)
|
||||||
s2 = s2.xor(s0)
|
s2 = s2.xor(s0)
|
||||||
@ -53,6 +53,10 @@ open class Xoshiro256PlusPlusRandom protected constructor(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextLong(): Long {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun raw(
|
fun raw(
|
||||||
|
@ -30,7 +30,7 @@ open class Xoshiro256StarStarRandom protected constructor(
|
|||||||
constructor(s0: Long, s1: Long, s2: Long, s3: Long) : this(s0, s1, s2, s3, null) {
|
constructor(s0: Long, s1: Long, s2: Long, s3: Long) : this(s0, s1, s2, s3, null) {
|
||||||
// discard some values so generator can get going if provided seed was "weak"
|
// discard some values so generator can get going if provided seed was "weak"
|
||||||
for (i in 0 until 32)
|
for (i in 0 until 32)
|
||||||
nextLong()
|
round()
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(seed: Long) : this(1L, 0L, 0L, 0L, null) {
|
constructor(seed: Long) : this(1L, 0L, 0L, 0L, null) {
|
||||||
@ -41,7 +41,7 @@ open class Xoshiro256StarStarRandom protected constructor(
|
|||||||
s3 = rng.nextLong()
|
s3 = rng.nextLong()
|
||||||
}
|
}
|
||||||
|
|
||||||
final override fun nextLong(): Long {
|
protected fun round(): Long {
|
||||||
val result = (s1 * 5).rotateLeft(7) * 9
|
val result = (s1 * 5).rotateLeft(7) * 9
|
||||||
val t = s1.shl(17)
|
val t = s1.shl(17)
|
||||||
s2 = s2.xor(s0)
|
s2 = s2.xor(s0)
|
||||||
@ -53,6 +53,10 @@ open class Xoshiro256StarStarRandom protected constructor(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextLong(): Long {
|
||||||
|
return round()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun raw(
|
fun raw(
|
||||||
|
@ -3,7 +3,6 @@ package ru.dbotthepony.kommons.test
|
|||||||
import it.unimi.dsi.fastutil.io.FastByteArrayInputStream
|
import it.unimi.dsi.fastutil.io.FastByteArrayInputStream
|
||||||
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
|
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import ru.dbotthepony.kommons.io.readVarInt
|
|
||||||
import ru.dbotthepony.kommons.io.readVarIntInfo
|
import ru.dbotthepony.kommons.io.readVarIntInfo
|
||||||
import ru.dbotthepony.kommons.io.writeVarInt
|
import ru.dbotthepony.kommons.io.writeVarInt
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
Loading…
Reference in New Issue
Block a user