June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,13 +1,30 @@
|
|||
// version 1.0.6
|
||||
// version 1.2.10
|
||||
|
||||
import java.util.Random
|
||||
|
||||
/**
|
||||
* Extension function on any list that will return a random element from index 0
|
||||
* to the last index
|
||||
*/
|
||||
fun <E> List<E>.getRandomElement() = this[Random().nextInt(this.size)]
|
||||
|
||||
/**
|
||||
* Extension function on any list that will return a list of unique random picks
|
||||
* from the list. If the specified number of elements you want is larger than the
|
||||
* number of elements in the list it returns null
|
||||
*/
|
||||
fun <E> List<E>.getRandomElements(numberOfElements: Int): List<E>? {
|
||||
if (numberOfElements > this.size) {
|
||||
return null
|
||||
}
|
||||
return this.shuffled().take(numberOfElements)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val list = listOf(1, 16, 3, 7, 17, 24, 34, 23, 11, 2)
|
||||
println("The list consists of the following numbers:")
|
||||
println(list)
|
||||
val chosen = mutableSetOf<Int>()
|
||||
println("\nFive elements chosen at random without duplication are:")
|
||||
while (chosen.size < 5) {
|
||||
val r = list[(Math.random() * list.size).toInt()]
|
||||
if (chosen.add(r)) println(r)
|
||||
}
|
||||
println("The list consists of the following numbers:\n${list}")
|
||||
|
||||
// notice we can call our extension functions as if they were regular member functions of List
|
||||
println("\nA randomly selected element from the list is ${list.getRandomElement()}")
|
||||
println("\nA random sequence of 5 elements from the list is ${list.getRandomElements(5)}")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue