skip to content

Random

kotlin.random.Random works across platforms, takes a seed when you need reproducibility, and backs the random() and shuffled() collection extensions. Seeding for deterministic tests is the practical point.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

How do you generate a random integer within a specific range (e.g. 1 to 6 inclusive) using Kotlin's standard library, and which class do you use?

level: juniorimportance: must knowfreq 70%

answer

  1. from inclusive, until exclusive
  2. nextInt(1,7) for a die
  3. (1..6).random() is inclusive both ends
  4. Random object = Default generator
  5. multiplatform, not java.util.Random

basics

~10 s

Use kotlin.random.Random. Call Random.nextInt(1, 7) to get a number from 1 up to but not including 7. The lower bound is included, the upper bound is excluded.

solid answer

~30 s

Kotlin provides kotlin.random.Random, a multiplatform random generator usable from common code (no java.util.Random needed). For a die roll use Random.nextInt(1, 7): the from bound is inclusive, the until bound is exclusive, so it yields 1..6. There is also nextInt(bound) which returns 0 until bound, and nextInt() for the full Int range. Random is an object you can call statically (Random.nextInt(...)) backed by a default thread-safe generator. For doubles use nextDouble(until) or nextDouble(from, until). Calling on an IntRange via (1..6).random() is the idiomatic shortcut; it internally validates the range is non-empty and uses the default generator.

code

kotlin · 4 lines
kotlin
import kotlin.random.Random

fun rollDie(): Int = Random.nextInt(1, 7) // 1..6
fun rollDieRange(): Int = (1..6).random() // also 1..6

go deeper

for a junior

Knows Random.nextInt(from, until) and that the lower bound is inclusive, upper exclusive.

for a middle

Distinguishes nextInt half-open semantics from the closed (1..6).random() shortcut and avoids off-by-one.

for a senior

Recommends kotlin.random.Random over java.util.Random for multiplatform and knows the empty-range exceptions.

for a principal

Frames generator choice (default vs seeded) and bound semantics as part of an API contract worth encoding in helper functions/tests.

## The `kotlin.random.Random` API `kotlin.random.Random` is Kotlin's **multiplatform** pseudo-random number generator. Unlike `java.util.Random`, it works in common code that compiles to JVM, JS, and Native, so prefer it in shared modules. `Random` is both an **abstract class** (you can subclass it) and has a **companion `Default`** instance exposed as the `Random` object itself, so `Random.nextInt(...)` works without constructing anything. ### Generating an Int in a range There are three overloads of `nextInt`: - `nextInt()` — any `Int` in the full range `Int.MIN_VALUE..Int.MAX_VALUE`. - `nextInt(until: Int)` — `0` (inclusive) up to `until` (**exclusive**). `until` must be positive. - `nextInt(from: Int, until: Int)` — `from` (inclusive) up to `until` (**exclusive**). The key rule: **`from` is inclusive, `until` is exclusive** (half-open interval). For a six-sided die you therefore write `Random.nextInt(1, 7)` to get `1..6`. ```kotlin import kotlin.random.Random val die = Random.nextInt(1, 7) // 1..6 inclusive val index = Random.nextInt(list.size) // 0 until size — safe array index val anyInt = Random.nextInt() // full Int range ``` ### The idiomatic range shortcut Kotlin adds `random()` extensions on `IntRange` / `LongRange`: ```kotlin val die = (1..6).random() // inclusive on BOTH ends because the range is closed ``` Note the subtlety: `(1..6)` is a **closed** range so `.random()` returns `1..6` inclusive, whereas `nextInt(1, 6)` is `1..5`. Mixing these up is a classic off-by-one bug. ### Errors Passing an empty or invalid range (e.g. `nextInt(5, 5)` or `nextInt(-1)`) throws `IllegalArgumentException`.

  • What is the difference between Random.nextInt(1, 6) and (1..6).random()?
    nextInt(1, 6) is half-open and yields 1..5; (1..6).random() uses a closed range and yields 1..6 inclusive.
  • What happens if you call Random.nextInt(0)?
    It throws IllegalArgumentException because the bound must be positive — the range 0 until 0 is empty.

Like picking a ticket from a numbered bin: nextInt(1,7) stocks tickets 1 through 6 — the '7' shelf stays empty.

saying these in an interview costs you the question

  • Claiming both bounds of nextInt(from, until) are inclusive
  • Reaching for java.util.Random in multiplatform/common code
  • Saying nextInt(6) can return 6
  • Thinking (1..6).random() excludes 6

context

open as a page

What do the random() and shuffled() collection extensions do in Kotlin, and how can you control which generator they use?

level: middleimportance: should knowfreq 60%

basics

~10 s

list.random() returns one random element; list.shuffled() returns a new list with the elements in random order. Both have overloads taking a Random instance so you can pass a seeded generator.

open as a page

How do you make random number generation reproducible in Kotlin, and why would you want a seeded generator instead of the default one?

level: middleimportance: should knowfreq 55%

basics

~10 s

Create a generator with a fixed seed using Random(seed). Two generators built with the same seed produce the same sequence of numbers, which is great for repeatable tests and debugging.

open as a page

When is kotlin.random.Random inappropriate, and what concurrency and security considerations apply when using random generators in production Kotlin code?

level: seniorimportance: should knowfreq 45%

basics

~10 s

kotlin.random.Random is predictable, so never use it for passwords, tokens, or keys — use a secure generator like SecureRandom. Also, the default generator is thread-safe but a custom seeded one may not be.

open as a page

Beyond nextInt, what other primitive random values can kotlin.random.Random produce, and what are the bound semantics for nextDouble?

level: middleimportance: nice to knowfreq 40%

basics

~10 s

Random can produce Long, Double, Float, Boolean, Int, and random bytes. nextDouble() returns a value from 0.0 up to (but not including) 1.0; overloads let you set bounds.

open as a page