Randomness is Not Chaos: How Kotlin Solves the Stack Overflow Problem
\n\nEvery 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\nIn 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\nBasics: Random in Kotlin
\n\nKotlin 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.
The Simplest Way: Random.nextInt()
\n\nIf you just need a random integer (from 0 to the maximum Int value), use:
\n\nimport kotlin.random.Random\n\nfun main() {\n val randomNumber = Random.nextInt()\n println("Random number: $randomNumber")\n}\n\nEach run will produce a new value. But more often we need to limit the range. For this, there is an overloaded version:
\n\nval fromZeroToTen = Random.nextInt(10) // 0..9\nval fromFiveToFifteen = Random.nextInt(5, 15) // 5..14\n\nImportant: In Kotlin, as in Java, the upper bound is exclusive. Random.nextInt(10) returns a number from 0 to 9.\n\nFractional Numbers: Random.nextDouble() and Random.nextFloat()
\n\nTo generate a random float in Kotlin or double, use:
\n\nval 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\nNote: nextFloat() does not support a range — only nextDouble() does. If you need a float in a range, convert it:
val customFloat = Random.nextDouble(1.5, 5.5).toFloat()\n\nGenerating Random Numbers in a Range: Practical Examples
\n\nMost often in real projects, you need a random number in a given range in Kotlin. Let's break down the most popular scenarios.
\n\nRandom Integer from 1 to 100 (inclusive)
\n\nTo include the upper bound, simply add 1:
\n\nval diceRoll = Random.nextInt(1, 7) // 1..6 (like a die)\nval lottery = Random.nextInt(1, 101) // 1..100\n\nRandom Index of an Array or List
\n\nOne of the most common tasks is to get a random element from a collection:
\n\nval colors = listOf("Red", "Blue", "Green", "Yellow")\nval randomColor = colors[Random.nextInt(colors.size)]\nprintln("Random color: $randomColor")\n\nBut in Kotlin, there is an even more elegant way — the random() function:
val randomColor = colors.random()\nprintln("Random color: $randomColor")\n\nUnder the hood, it does the same thing, but the code becomes cleaner.
\n\nRandom Number with a Step
\n\nSometimes you need to generate numbers that are multiples of a certain value. For example, even numbers from 0 to 100:
\n\nfun 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\nAdvanced Techniques: Sequences and Cryptographic Strength
\n\nRandom Sequence of Numbers
\n\nIf you need to generate a list of random numbers, use generate