From 7e0a658de7ccc3dd12d7a589fdd2d3f7f653e431 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 12 Sep 2018 21:37:46 +0300 Subject: [PATCH] Change nextInt/Long with range parameters from members to extensions We'd like to fix the implementation of these methods, so we could intrinsify them later or perform other optimizations that are not possible when these methods are open. --- libraries/stdlib/src/kotlin/random/Random.kt | 65 +++++++++---------- .../kotlin-stdlib-runtime-merged.txt | 6 +- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/libraries/stdlib/src/kotlin/random/Random.kt b/libraries/stdlib/src/kotlin/random/Random.kt index 4a64583a0fa..7d72e045485 100644 --- a/libraries/stdlib/src/kotlin/random/Random.kt +++ b/libraries/stdlib/src/kotlin/random/Random.kt @@ -78,21 +78,6 @@ public abstract class Random { } } - /** - * Gets the next random `Int` from the random number generator in the specified [range]. - * - * Generates an `Int` random value uniformly distributed in the specified [range]: - * from `range.start` inclusive to `range.endInclusive` inclusive. - * - * @throws IllegalArgumentException if [range] is empty. - */ - public open fun nextInt(range: IntRange): Int = when { - range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") - range.last < Int.MAX_VALUE -> nextInt(range.first, range.last + 1) - range.first > Int.MIN_VALUE -> nextInt(range.first - 1, range.last) + 1 - else -> nextInt() - } - /** * Gets the next random `Long` from the random number generator. * @@ -157,21 +142,6 @@ public abstract class Random { } } - /** - * Gets the next random `Long` from the random number generator in the specified [range]. - * - * Generates a `Long` random value uniformly distributed in the specified [range]: - * from `range.start` inclusive to `range.endInclusive` inclusive. - * - * @throws IllegalArgumentException if [range] is empty. - */ - public open fun nextLong(range: LongRange): Long = when { - range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") - range.last < Long.MAX_VALUE -> nextLong(range.start, range.endInclusive + 1) - range.start > Long.MIN_VALUE -> nextLong(range.start - 1, range.endInclusive) + 1 - else -> nextLong() - } - /** * Gets the next random [Boolean] value. */ @@ -276,12 +246,10 @@ public abstract class Random { override fun nextInt(): Int = defaultRandom.nextInt() override fun nextInt(until: Int): Int = defaultRandom.nextInt(until) override fun nextInt(from: Int, until: Int): Int = defaultRandom.nextInt(from, until) - override fun nextInt(range: IntRange): Int = defaultRandom.nextInt(range) override fun nextLong(): Long = defaultRandom.nextLong() override fun nextLong(until: Long): Long = defaultRandom.nextLong(until) override fun nextLong(from: Long, until: Long): Long = defaultRandom.nextLong(from, until) - override fun nextLong(range: LongRange): Long = defaultRandom.nextLong(range) override fun nextBoolean(): Boolean = defaultRandom.nextBoolean() @@ -295,7 +263,6 @@ public abstract class Random { override fun nextBytes(size: Int): ByteArray = defaultRandom.nextBytes(size) override fun nextBytes(array: ByteArray, fromIndex: Int, toIndex: Int): ByteArray = defaultRandom.nextBytes(array, fromIndex, toIndex) - } } @@ -321,6 +288,38 @@ public fun Random(seed: Long): Random = XorWowRandom(seed.toInt(), seed.shr(32). +/** + * Gets the next random `Int` from the random number generator in the specified [range]. + * + * Generates an `Int` random value uniformly distributed in the specified [range]: + * from `range.start` inclusive to `range.endInclusive` inclusive. + * + * @throws IllegalArgumentException if [range] is empty. + */ +@SinceKotlin("1.3") +public fun Random.nextInt(range: IntRange): Int = when { + range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") + range.last < Int.MAX_VALUE -> nextInt(range.first, range.last + 1) + range.first > Int.MIN_VALUE -> nextInt(range.first - 1, range.last) + 1 + else -> nextInt() +} + +/** + * Gets the next random `Long` from the random number generator in the specified [range]. + * + * Generates a `Long` random value uniformly distributed in the specified [range]: + * from `range.start` inclusive to `range.endInclusive` inclusive. + * + * @throws IllegalArgumentException if [range] is empty. + */ +@SinceKotlin("1.3") +public fun Random.nextLong(range: LongRange): Long = when { + range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") + range.last < Long.MAX_VALUE -> nextLong(range.start, range.endInclusive + 1) + range.start > Long.MIN_VALUE -> nextLong(range.start - 1, range.endInclusive) + 1 + else -> nextLong() +} + /** * Gets the next random [UInt] from the random number generator. * diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 518abb629ce..ff19de93830 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -3916,11 +3916,9 @@ public abstract class kotlin/random/Random { public fun nextInt ()I public fun nextInt (I)I public fun nextInt (II)I - public fun nextInt (Lkotlin/ranges/IntRange;)I public fun nextLong ()J public fun nextLong (J)J public fun nextLong (JJ)J - public fun nextLong (Lkotlin/ranges/LongRange;)J } public final class kotlin/random/Random$Companion : kotlin/random/Random { @@ -3936,16 +3934,16 @@ public final class kotlin/random/Random$Companion : kotlin/random/Random { public fun nextInt ()I public fun nextInt (I)I public fun nextInt (II)I - public fun nextInt (Lkotlin/ranges/IntRange;)I public fun nextLong ()J public fun nextLong (J)J public fun nextLong (JJ)J - public fun nextLong (Lkotlin/ranges/LongRange;)J } public final class kotlin/random/RandomKt { public static final fun Random (I)Lkotlin/random/Random; public static final fun Random (J)Lkotlin/random/Random; + public static final fun nextInt (Lkotlin/random/Random;Lkotlin/ranges/IntRange;)I + public static final fun nextLong (Lkotlin/random/Random;Lkotlin/ranges/LongRange;)J public static final fun nextUBytes (Lkotlin/random/Random;I)[B public static final fun nextUBytes-EVgfTAA (Lkotlin/random/Random;[B)[B public static final fun nextUBytes-Wvrt4B4 (Lkotlin/random/Random;[BII)[B