More random generator compat with original engine
This commit is contained in:
parent
032f32626e
commit
96fdcccdd0
@ -203,11 +203,31 @@ fun RandomGenerator.nextBytes(length: Int): ByteArray {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Collection<T>.random(random: RandomGenerator): T {
|
// for randomgen compat only
|
||||||
|
@Deprecated("Delicate API, use only when required for compatibility with original protocol")
|
||||||
|
fun RandomGenerator.randu64(): ULong {
|
||||||
|
val a = nextInt().toLong() and 0xFFFFFFFFL
|
||||||
|
val b = nextInt().toLong() and 0xFFFFFFFFL
|
||||||
|
return a.toULong().shl(32) or b.toULong()
|
||||||
|
}
|
||||||
|
|
||||||
|
// for randomgen compat only
|
||||||
|
@Deprecated("Delicate API, use only when required for compatibility with original protocol")
|
||||||
|
fun RandomGenerator.nextUInt(max: ULong): ULong {
|
||||||
|
val denom = ULong.MAX_VALUE / (max + 1UL)
|
||||||
|
return randu64() / denom
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> Collection<T>.random(random: RandomGenerator, legacyCompatibleSelection: Boolean = false): T {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw NoSuchElementException("List is empty")
|
throw NoSuchElementException("List is empty")
|
||||||
|
|
||||||
return elementAt(random.nextInt(size))
|
if (legacyCompatibleSelection) {
|
||||||
|
// marvelous
|
||||||
|
return elementAt(random.nextUInt(size.toULong()).toInt())
|
||||||
|
} else {
|
||||||
|
return elementAt(random.nextInt(size))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun JsonArray.random(random: RandomGenerator): JsonElement {
|
fun JsonArray.random(random: RandomGenerator): JsonElement {
|
||||||
|
Loading…
Reference in New Issue
Block a user