Add shuffle variant for object array
This commit is contained in:
parent
9e1ae327ea
commit
578d86410b
@ -78,6 +78,17 @@ fun IntArray.shuffle(random: RandomSource): IntArray {
|
|||||||
return this
|
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 {
|
fun <L : IntList> L.shuffle(random: RandomSource): L {
|
||||||
for (i in lastIndex downTo 1) {
|
for (i in lastIndex downTo 1) {
|
||||||
val j = random.nextInt(i + 1)
|
val j = random.nextInt(i + 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user