How to Get a Random Number in Kotlin: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Randomness is Not Chaos: How Kotlin Solves the Stack Overflow Problem

\n\n

Every developer has written code to generate a random number at least once in their life. It seems simple, right? But when this question is asked on Stack Overflow (and the question "How can I get a random number in Kotlin?" gathers thousands of views), it becomes clear: there are more pitfalls than it seems. Especially if you are coming from Java or Python.

\n\n

In this article, we will cover random number generation in Kotlin from simple to advanced techniques. You will learn how to get a random integer, a fractional number, how to set a range, and even how to generate cryptographically secure random for security. Let's go!

\n\n

Basics: Random in Kotlin

\n\n

Kotlin does not have its own separate class for random — it uses the powerful java.util.Random from Java, but with a much more convenient syntax. Thanks to extension functions, you can write Kotlin random code literally in one line.

\n\n

The Simplest Way: Random.nextInt()

\n\n

If you just need a random integer (from 0 to the maximum Int value), use:

\n\n
import kotlin.random.Random\n\nfun main() {\n    val randomNumber = Random.nextInt()\n    println("Random number: $randomNumber")\n}
\n\n

Each run will produce a new value. But more often we need to limit the range. For this, there is an overloaded version:

\n\n
val fromZeroToTen = Random.nextInt(10)   // 0..9\nval fromFiveToFifteen = Random.nextInt(5, 15) // 5..14
\n\n
Important: In Kotlin, as in Java, the upper bound is exclusive. Random.nextInt(10) returns a number from 0 to 9.
\n\n

Fractional Numbers: Random.nextDouble() and Random.nextFloat()

\n\n

To generate a random float in Kotlin or double, use:

\n\n
val randomDouble = Random.nextDouble()   // 0.0..1.0\nval randomFloat = Random.nextFloat()     // 0.0..1.0\n\n// With a range:\nval doubleInRange = Random.nextDouble(10.0, 20.0) // 10.0..20.0
\n\n

Note: nextFloat() does not support a range — only nextDouble() does. If you need a float in a range, convert it:

\n\n
val customFloat = Random.nextDouble(1.5, 5.5).toFloat()
\n\n

Generating Random Numbers in a Range: Practical Examples

\n\n

Most often in real projects, you need a random number in a given range in Kotlin. Let's break down the most popular scenarios.

\n\n

Random Integer from 1 to 100 (inclusive)

\n\n

To include the upper bound, simply add 1:

\n\n
val diceRoll = Random.nextInt(1, 7)  // 1..6 (like a die)\nval lottery = Random.nextInt(1, 101) // 1..100
\n\n

Random Index of an Array or List

\n\n

One of the most common tasks is to get a random element from a collection:

\n\n
val colors = listOf("Red", "Blue", "Green", "Yellow")\nval randomColor = colors[Random.nextInt(colors.size)]\nprintln("Random color: $randomColor")
\n\n

But in Kotlin, there is an even more elegant way — the random() function:

\n\n
val randomColor = colors.random()\nprintln("Random color: $randomColor")
\n\n

Under the hood, it does the same thing, but the code becomes cleaner.

\n\n

Random Number with a Step

\n\n

Sometimes you need to generate numbers that are multiples of a certain value. For example, even numbers from 0 to 100:

\n\n
fun randomEven(max: Int): Int {\n    val half = max / 2\n    return Random.nextInt(0, half + 1) * 2\n}\n\nprintln(randomEven(100)) // 0, 2, 4, ..., 100
\n\n

Advanced Techniques: Sequences and Cryptographic Strength

\n\n

Random Sequence of Numbers

\n\n

If you need to generate a list of random numbers, use generate

Blogs

Book Recommendations