diff --git a/runtime/src/main/kotlin/kotlin/random/Random.kt b/runtime/src/main/kotlin/kotlin/random/Random.kt index 7b6b7f13d96..79ca36d3970 100644 --- a/runtime/src/main/kotlin/kotlin/random/Random.kt +++ b/runtime/src/main/kotlin/kotlin/random/Random.kt @@ -7,7 +7,7 @@ package kotlin.random import kotlin.native.concurrent.AtomicLong import kotlin.system.getTimeNanos -public abstract class NativeRandom { +internal class NativeRandom { /** * A default pseudo-random linear congruential generator. */ @@ -28,39 +28,12 @@ public abstract class NativeRandom { _seed.value = seed } - /** - * Returns a pseudo-random Int number. - */ - override fun nextInt(): Int { - update((seed * MULTIPLIER + 0xbL) and ((1L shl 48) - 1)); - return (seed ushr 16).toInt(); + override fun nextBits(bitCount: Int): Int { + update((seed * MULTIPLIER + 0xbL) and ((1L shl 48) - 1)) + return (seed ushr (48 - bitCount)).toInt() } - /** - * Returns a pseudo-random Int value between 0 and specified value (exclusive) - */ - override fun nextInt(bound: Int): Int { - if (bound <= 0) throw IllegalArgumentException("Incorrect bound: $bound") - - if (bound and (bound - 1) == 0) { - return ((bound * (nextInt() ushr 1).toLong()) ushr 31).toInt(); - } - - var m: Int - do { - var r = nextInt() ushr 1 - m = r % bound - } while (r - m + (bound - 1) < 0) - return m - } - - /** - * Returns a pseudo-random Long number. - */ - override fun nextLong(): Long = (nextInt().toLong() shl 32) + nextInt().toLong() - - override fun nextBits(bitCount: Int): Int = - nextInt().takeUpperBits(bitCount) + override fun nextInt(): Int = nextBits(32) } }