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?
answer
- from inclusive, until exclusive
- nextInt(1,7) for a die
- (1..6).random() is inclusive both ends
- Random object = Default generator
- multiplatform, not java.util.Random
basics
~10 sUse 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 sKotlin 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 linesimport kotlin.random.Random
fun rollDie(): Int = Random.nextInt(1, 7) // 1..6
fun rollDieRange(): Int = (1..6).random() // also 1..6go deeper
Knows Random.nextInt(from, until) and that the lower bound is inclusive, upper exclusive.
Distinguishes nextInt half-open semantics from the closed (1..6).random() shortcut and avoids off-by-one.
Recommends kotlin.random.Random over java.util.Random for multiplatform and knows the empty-range exceptions.
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