nextLong

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

Since Kotlin

1.3

Samples

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
}

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

Since Kotlin

1.3

Parameters

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
}

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

Since Kotlin

1.3

Throws

if from is greater than or equal to until.

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
}