nextLong
Gets the next random Long
from the random number generator.
Generates a Long
random value uniformly distributed between Long.MIN_VALUE
and Long.MAX_VALUE
(inclusive).
Since Kotlin
1.3Samples
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue
fun main() {
//sampleStart
val randomLongs = List(5) { Random.nextLong() }
println(randomLongs)
val sortedRandomLongs = randomLongs.sorted()
println(sortedRandomLongs)
//sampleEnd
}
Gets the next random non-negative Long
from the random number generator less than the specified until bound.
Generates a Long
random value uniformly distributed between 0
(inclusive) and the specified until bound (exclusive).
Since Kotlin
1.3Parameters
until
must be positive.
Throws
if until is negative or zero.
Samples
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue
fun main() {
//sampleStart
val fileSize = Random.nextLong(until = 1_099_511_627_776)
println("A file of $fileSize bytes fits on a 1TB storage.")
val long = Random.nextLong(Int.MAX_VALUE + 1L, Long.MAX_VALUE)
println("Number $long doesn't fit in an Int.")
//sampleEnd
}
Gets the next random Long
from the random number generator in the specified range.
Generates a Long
random value uniformly distributed between the specified from (inclusive) and until (exclusive) bounds.
Since Kotlin
1.3Throws
Samples
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue
fun main() {
//sampleStart
val fileSize = Random.nextLong(until = 1_099_511_627_776)
println("A file of $fileSize bytes fits on a 1TB storage.")
val long = Random.nextLong(Int.MAX_VALUE + 1L, Long.MAX_VALUE)
println("Number $long doesn't fit in an Int.")
//sampleEnd
}