Add shuffle variant for object array

This commit is contained in:
DBotThePony 2025-04-03 16:35:47 +07:00
parent 9e1ae327ea
commit 578d86410b
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -78,6 +78,17 @@ fun IntArray.shuffle(random: RandomSource): IntArray {
return this
}
fun <T> Array<T>.shuffle(random: RandomSource): Array<T> {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
return this
}
fun <L : IntList> L.shuffle(random: RandomSource): L {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)