Random

abstract class Random

An abstract class that is implemented by random number generator algorithms.

The companion object Random.Default is the default instance of Random.

To get a seeded instance of random generator use Random function.

Since Kotlin

1.3

Samples

import samples.*
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue
fun main() { 
   //sampleStart 
   val randomValues = List(10) { Random.nextInt(0, 100) }
// prints new sequence every time
println(randomValues)

val nextValues = List(10) { Random.nextInt(0, 100) }
println(nextValues)
assertTrue(randomValues != nextValues) 
   //sampleEnd
}

Constructors

Random
Link copied to clipboard
fun Random()

Types

Default
Link copied to clipboard
object Default : Random, Serializable

The default random number generator.

Functions

nextBits
Link copied to clipboard
abstract fun nextBits(bitCount: Int): Int

Gets the next random bitCount number of bits.

nextBoolean
Link copied to clipboard
open fun nextBoolean(): Boolean

Gets the next random Boolean value.

nextBytes
Link copied to clipboard
open fun nextBytes(array: ByteArray): ByteArray

Fills the specified byte array with random bytes and returns it.

open fun nextBytes(size: Int): ByteArray

Creates a byte array of the specified size, filled with random bytes.

open fun nextBytes(array: ByteArray, fromIndex: Int = 0, toIndex: Int = array.size): ByteArray

Fills a subrange of the specified byte array starting from fromIndex inclusive and ending toIndex exclusive with random bytes.

nextDouble
Link copied to clipboard
open fun nextDouble(): Double

Gets the next random Double value uniformly distributed between 0 (inclusive) and 1 (exclusive).

open fun nextDouble(until: Double): Double

Gets the next random non-negative Double from the random number generator less than the specified until bound.

open fun nextDouble(from: Double, until: Double): Double

Gets the next random Double from the random number generator in the specified range.

nextFloat
Link copied to clipboard
open fun nextFloat(): Float

Gets the next random Float value uniformly distributed between 0 (inclusive) and 1 (exclusive).

nextInt
Link copied to clipboard
open fun nextInt(): Int

Gets the next random Int from the random number generator.

open fun nextInt(until: Int): Int

Gets the next random non-negative Int from the random number generator less than the specified until bound.

open fun nextInt(from: Int, until: Int): Int

Gets the next random Int from the random number generator in the specified range.

nextLong
Link copied to clipboard
open fun nextLong(): Long

Gets the next random Long from the random number generator.

open fun nextLong(until: Long): Long

Gets the next random non-negative Long from the random number generator less than the specified until bound.

open fun nextLong(from: Long, until: Long): Long

Gets the next random Long from the random number generator in the specified range.

Inheritors

Default
Link copied to clipboard

Extensions

asJavaRandom
Link copied to clipboard
fun Random.asJavaRandom(): Random

Creates a java.util.Random instance that uses the specified Kotlin Random generator as a randomness source.

nextInt
Link copied to clipboard
fun Random.nextInt(range: IntRange): Int

Gets the next random Int from the random number generator in the specified range.

nextLong
Link copied to clipboard
fun Random.nextLong(range: LongRange): Long

Gets the next random Long from the random number generator in the specified range.

nextUBytes
Link copied to clipboard
fun Random.nextUBytes(array: UByteArray): UByteArray

Fills the specified unsigned byte array with random bytes and returns it.

fun Random.nextUBytes(size: Int): UByteArray

Creates an unsigned byte array of the specified size, filled with random bytes.

fun Random.nextUBytes(array: UByteArray, fromIndex: Int = 0, toIndex: Int = array.size): UByteArray

Fills a subrange of the specified UBytearray starting from fromIndex inclusive and ending toIndex exclusive with random UBytes.

nextUInt
Link copied to clipboard
fun Random.nextUInt(): UInt

Gets the next random UInt from the random number generator.

fun Random.nextUInt(until: UInt): UInt

Gets the next random UInt from the random number generator less than the specified until bound.

fun Random.nextUInt(from: UInt, until: UInt): UInt

Gets the next random UInt from the random number generator in the specified range.

fun Random.nextUInt(range: UIntRange): UInt

Gets the next random UInt from the random number generator in the specified range.

nextULong
Link copied to clipboard
fun Random.nextULong(): ULong

Gets the next random ULong from the random number generator.

fun Random.nextULong(until: ULong): ULong

Gets the next random ULong from the random number generator less than the specified until bound.

fun Random.nextULong(from: ULong, until: ULong): ULong

Gets the next random ULong from the random number generator in the specified range.

fun Random.nextULong(range: ULongRange): ULong

Gets the next random ULong from the random number generator in the specified range.