From 0fe785de96ef1162691bff059f3262f6034ca88f Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 2 Oct 2018 02:37:56 +0300 Subject: [PATCH] Remove the ability to change seed of the default Random --- backend.native/tests/runtime/basic/random.kt | 36 +++++++++---------- .../tests/runtime/basic/worker_random.kt | 1 - .../src/main/kotlin/kotlin/random/Random.kt | 8 +---- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/backend.native/tests/runtime/basic/random.kt b/backend.native/tests/runtime/basic/random.kt index 422d9f126b6..8afeac16175 100644 --- a/backend.native/tests/runtime/basic/random.kt +++ b/backend.native/tests/runtime/basic/random.kt @@ -13,14 +13,14 @@ import kotlin.test.* /** * Tests that setting the same seed make random generate the same sequence */ -private inline fun testReproducibility(seed: Long, generator: () -> T) { +private inline fun testReproducibility(seed: Long, generator: Random.() -> T) { // Reset seed. This will make Random to start a new sequence - Random.seed = seed - val first = Array(50, { i -> generator() }).toList() + val r1 = Random(seed) + val first = Array(50, { i -> r1.generator() }).toList() // Reset seed and try again - Random.seed = seed - val second = Array(50, { i -> generator() }).toList() + val r2 = Random(seed) + val second = Array(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 testReproducibility(seed: Long, generator: () -> /** * Tests that setting seed makes random generate different sequence. */ -private inline fun testDifference(generator: () -> T) { - Random.seed = 12345678L - val first = Array(100, { i -> generator() }).toList() +private inline fun testDifference(generator: Random.() -> T) { + val r1 = Random(12345678L) + val first = Array(100, { i -> r1.generator() }).toList() - Random.seed = 87654321L - val second = Array(100, { i -> generator() }).toList() + val r2 = Random(87654321L) + val second = Array(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) }) } diff --git a/backend.native/tests/runtime/basic/worker_random.kt b/backend.native/tests/runtime/basic/worker_random.kt index f941d397c3e..2aaf1515066 100644 --- a/backend.native/tests/runtime/basic/worker_random.kt +++ b/backend.native/tests/runtime/basic/worker_random.kt @@ -19,7 +19,6 @@ fun testRandomWorkers() { val attempts = 3 val results = Array(attempts, { ArrayList() } ) 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 }) { diff --git a/runtime/src/main/kotlin/kotlin/random/Random.kt b/runtime/src/main/kotlin/kotlin/random/Random.kt index 487341f5dba..ee2b4e56cb5 100644 --- a/runtime/src/main/kotlin/kotlin/random/Random.kt +++ b/runtime/src/main/kotlin/kotlin/random/Random.kt @@ -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 } \ No newline at end of file