/** Fisher-Yates with SplitMix64. Our own PRNG so the order never changes across Kotlin versions. */
fun seededShuffle(items: List<String>, seed: Long): List<String> {
val out = items.toMutableList()
val rng = SplitMix64(seed)
for (i in out.lastIndex downTo 1) {
val j = rng.nextInt(i + 1)
val tmp = out[i]; out[i] = out[j]; out[j] = tmp
}
return out
}
internal class SplitMix64(private var state: Long) {
fun nextLong(): Long {
state += GOLDEN_GAMMA
var z = state
z = (z xor (z ushr 30)) * MIX_1
z = (z xor (z ushr 27)) * MIX_2
return z xor (z ushr 31)
}
/** Uniform enough for shuffling a few hundred ids; bias is below 2^-50. */
fun nextInt(bound: Int): Int = ((nextLong() ushr 1) % bound).toInt()
private companion object {
val GOLDEN_GAMMA = 0x9E3779B97F4A7C15uL.toLong()
val MIX_1 = 0xBF58476D1CE4E5B9uL.toLong()
val MIX_2 = 0x94D049BB133111EBuL.toLong()
}
}