Make NativeRandom an object instead of a class with companion
This commit is contained in:
committed by
Pavel Punegov
parent
8e4806725b
commit
71a601d87e
@@ -7,34 +7,32 @@ package kotlin.random
|
|||||||
import kotlin.native.concurrent.AtomicLong
|
import kotlin.native.concurrent.AtomicLong
|
||||||
import kotlin.system.getTimeNanos
|
import kotlin.system.getTimeNanos
|
||||||
|
|
||||||
internal class NativeRandom {
|
/**
|
||||||
|
* The default implementation of pseudo-random generator using the linear congruential generator.
|
||||||
|
*/
|
||||||
|
internal object NativeRandom : Random() {
|
||||||
|
private const val MULTIPLIER = 0x5deece66dL
|
||||||
|
private val _seed = AtomicLong(mult(getTimeNanos()))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A default pseudo-random linear congruential generator.
|
* Random generator seed value.
|
||||||
*/
|
*/
|
||||||
companion object : Random() {
|
var seed: Long
|
||||||
private const val MULTIPLIER = 0x5deece66dL
|
get() = _seed.value
|
||||||
private val _seed = AtomicLong(mult(getTimeNanos()))
|
set(value) = update(mult(value))
|
||||||
|
|
||||||
/**
|
private fun mult(value: Long) = (value xor MULTIPLIER) and ((1L shl 48) - 1)
|
||||||
* Random generator seed value.
|
|
||||||
*/
|
|
||||||
var seed: Long
|
|
||||||
get() = _seed.value
|
|
||||||
set(value) = update(mult(value))
|
|
||||||
|
|
||||||
private fun mult(value: Long) = (value xor MULTIPLIER) and ((1L shl 48) - 1)
|
private fun update(seed: Long): Unit {
|
||||||
|
_seed.value = seed
|
||||||
private fun update(seed: Long): Unit {
|
|
||||||
_seed.value = seed
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun nextBits(bitCount: Int): Int {
|
|
||||||
update((seed * MULTIPLIER + 0xbL) and ((1L shl 48) - 1))
|
|
||||||
return (seed ushr (48 - bitCount)).toInt()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun nextInt(): Int = nextBits(32)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun nextBits(bitCount: Int): Int {
|
||||||
|
update((seed * MULTIPLIER + 0xbL) and ((1L shl 48) - 1))
|
||||||
|
return (seed ushr (48 - bitCount)).toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun nextInt(): Int = nextBits(32)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal actual fun defaultPlatformRandom(): Random = NativeRandom
|
internal actual fun defaultPlatformRandom(): Random = NativeRandom
|
||||||
|
|||||||
Reference in New Issue
Block a user