Provide common Random API

#KT-17261
This commit is contained in:
Ilya Gorbunov
2018-07-03 03:43:34 +03:00
parent 9df411481c
commit cc031e3a40
9 changed files with 415 additions and 0 deletions
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package samples.random
import samples.*
import kotlin.random.Random
import kotlin.test.assertTrue
class Randoms {
@Sample
fun defaultRandom() {
val randomValues = List(10) { Random.nextInt(0, 100) }
// prints new sequence every time
println(randomValues)
val nextValues = List(10) { Random.nextInt(0, 100) }
println(nextValues)
assertTrue(randomValues != nextValues)
}
@Sample
fun seededRandom() {
fun getRandomList(random: Random): List<Int> =
List(10) { random.nextInt(0, 100) }
val randomValues1 = getRandomList(Random(42))
// prints the same sequence every time
assertPrints(randomValues1, "[44, 34, 69, 67, 22, 16, 67, 45, 95, 10]")
val randomValues2 = getRandomList(Random(42))
// random with the same seed produce the same sequence
assertTrue(randomValues1 == randomValues2)
val randomValues3 = getRandomList(Random(0))
// random with another seed produce another sequence
assertPrints(randomValues3, "[20, 63, 41, 28, 0, 99, 35, 42, 72, 13]")
}
}