-
Notifications
You must be signed in to change notification settings - Fork 86
Open
Description
fun <T:Comparable<T>>shuffle(items:MutableList<T>):List<T>{
val rg : Random = Random()
for (i in 0..items.size - 1) {
val randomPosition = rg.nextInt(items.size)
val tmp : T = items[i]
items[i] = items[randomPosition]
items[randomPosition] = tmp
}
return items
}
This algorithm doesn't shuffle it correctly, not every permutation is equiprobable. You have to do:
val randomPosition = rg.nextInt(i + 1)
to fix it.
More information:
https://blog.codinghorror.com/the-danger-of-naivete/
Metadata
Metadata
Assignees
Labels
No labels