Remove the ability to change seed of the default Random
This commit is contained in:
committed by
Pavel Punegov
parent
e98ef837b7
commit
0fe785de96
@@ -13,14 +13,14 @@ import kotlin.test.*
|
||||
/**
|
||||
* Tests that setting the same seed make random generate the same sequence
|
||||
*/
|
||||
private inline fun <reified T> testReproducibility(seed: Long, generator: () -> T) {
|
||||
private inline fun <reified T> testReproducibility(seed: Long, generator: Random.() -> T) {
|
||||
// Reset seed. This will make Random to start a new sequence
|
||||
Random.seed = seed
|
||||
val first = Array<T>(50, { i -> generator() }).toList()
|
||||
val r1 = Random(seed)
|
||||
val first = Array<T>(50, { i -> r1.generator() }).toList()
|
||||
|
||||
// Reset seed and try again
|
||||
Random.seed = seed
|
||||
val second = Array<T>(50, { i -> generator() }).toList()
|
||||
val r2 = Random(seed)
|
||||
val second = Array<T>(50, { i -> r2.generator() }).toList()
|
||||
assertTrue(first == second, "FAIL: got different sequences of generated values " +
|
||||
"first: $first, second: $second")
|
||||
}
|
||||
@@ -28,36 +28,36 @@ private inline fun <reified T> testReproducibility(seed: Long, generator: () ->
|
||||
/**
|
||||
* Tests that setting seed makes random generate different sequence.
|
||||
*/
|
||||
private inline fun <reified T> testDifference(generator: () -> T) {
|
||||
Random.seed = 12345678L
|
||||
val first = Array<T>(100, { i -> generator() }).toList()
|
||||
private inline fun <reified T> testDifference(generator: Random.() -> T) {
|
||||
val r1 = Random(12345678L)
|
||||
val first = Array<T>(100, { i -> r1.generator() }).toList()
|
||||
|
||||
Random.seed = 87654321L
|
||||
val second = Array<T>(100, { i -> generator() }).toList()
|
||||
val r2 = Random(87654321L)
|
||||
val second = Array<T>(100, { i -> r2.generator() }).toList()
|
||||
assertTrue(first != second, "FAIL: got the same sequence of generated values " +
|
||||
"first: $first, second: $second")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInts() {
|
||||
testReproducibility(getTimeMillis(), { Random.nextInt() })
|
||||
testReproducibility(Long.MAX_VALUE, { Random.nextInt() })
|
||||
testReproducibility(getTimeMillis(), { nextInt() })
|
||||
testReproducibility(Long.MAX_VALUE, { nextInt() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLong() {
|
||||
testReproducibility(getTimeMillis(), { Random.nextLong() })
|
||||
testReproducibility(Long.MAX_VALUE, { Random.nextLong() })
|
||||
testReproducibility(getTimeMillis(), { nextLong() })
|
||||
testReproducibility(Long.MAX_VALUE, { nextLong() })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDiffInt() = testDifference { Random.nextInt() }
|
||||
fun testDiffInt() = testDifference { nextInt() }
|
||||
|
||||
@Test
|
||||
fun testDiffLong() = testDifference { Random.nextLong() }
|
||||
fun testDiffLong() = testDifference { nextLong() }
|
||||
|
||||
@Test
|
||||
fun testNextInt() {
|
||||
testReproducibility(getTimeMillis(), { Random.nextInt(1000) })
|
||||
testReproducibility(1000L, { Random.nextInt(1024) })
|
||||
testReproducibility(getTimeMillis(), { nextInt(1000) })
|
||||
testReproducibility(1000L, { nextInt(1024) })
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ fun testRandomWorkers() {
|
||||
val attempts = 3
|
||||
val results = Array(attempts, { ArrayList<Int>() } )
|
||||
for (attempt in 0 until attempts) {
|
||||
Random.seed = seed
|
||||
// Produce a list of random numbers in each worker
|
||||
val futures = Array(workers.size, { workerIndex ->
|
||||
workers[workerIndex].execute(TransferMode.SAFE, { workerIndex }) {
|
||||
|
||||
@@ -17,9 +17,8 @@ internal object NativeRandom : Random() {
|
||||
/**
|
||||
* Random generator seed value.
|
||||
*/
|
||||
var seed: Long
|
||||
private val seed: Long
|
||||
get() = _seed.value
|
||||
set(value) = update(mult(value))
|
||||
|
||||
private fun mult(value: Long) = (value xor MULTIPLIER) and ((1L shl 48) - 1)
|
||||
|
||||
@@ -42,8 +41,3 @@ internal actual fun fastLog2(value: Int): Int =
|
||||
|
||||
internal actual fun doubleFromParts(hi26: Int, low27: Int): Double =
|
||||
(hi26.toLong().shl(27) + low27) / (1L shl 53).toDouble()
|
||||
|
||||
// TODO: common stdlib Random hasn't got seed, this is workaround for running K/N tests.
|
||||
var Random.seed:Long
|
||||
get() = NativeRandom.seed
|
||||
set(value) { NativeRandom.seed = value }
|
||||
Reference in New Issue
Block a user