Make NativeRandom internal, use inherited implementations

This commit is contained in:
Ilya Gorbunov
2018-10-01 23:13:11 +03:00
committed by Pavel Punegov
parent 081e4a7b12
commit ee37eef93c
@@ -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)
}
}