nextLong

Common
JVM
JS
Native
1.0
open fun nextLong(): Long
(source)
fun nextLong(): Long
(source)

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).

import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue

fun main(args: Array<String>) {
//sampleStart
val randomLongs = List(5) { Random.nextLong() }
println(randomLongs)
val sortedRandomLongs = randomLongs.sorted()
println(sortedRandomLongs)
//sampleEnd
}
Common
JVM
JS
Native
1.0
open fun nextLong(until: Long): Long
(source)
fun nextLong(until: Long): Long
(source)

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).

import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue

fun main(args: Array<String>) {
//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
}

Parameters

until - must be positive.

Exceptions

IllegalArgumentException - if until is negative or zero.

Common
JVM
JS
Native
1.0
open fun nextLong(from: Long, until: Long): Long
(source)
fun nextLong(from: Long, until: Long): Long
(source)

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.

import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue

fun main(args: Array<String>) {
//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
}

Exceptions

IllegalArgumentException - if from is greater than or equal to until.