Generate random numbers in Kotlin
This article explores different ways to generate random numbers between the specified range in Kotlin.
1. Using random() function
A simple approach to retrieve an arbitrary element from the specified range is to use the random() function.
|
1 2 3 4 5 6 7 8 9 10 11 |
fun rand(start: Int, end: Int): Int { require(start <= end) { "Illegal Argument" } return (start..end).random() } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
7
5
9
8
8
You can call it without arguments or with a Random object as a randomness source.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import kotlin.random.Random fun rand(start: Int, end: Int): Int { require(start <= end) { "Illegal Argument" } val rand = Random(System.nanoTime()) return (start..end).random(rand) } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
7
7
9
5
6
2. Using Random class
Kotlin offers the Random class in the kotlin.random package, which can generate random numbers. You can use its nextInt() function to get a pseudorandom integer value between 0 (inclusive) and the specified value (exclusive).
Following is a simple example demonstrating usage of this function, which generates a pseudorandom number between start and end:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import kotlin.random.Random fun rand(start: Int, end: Int): Int { require(!(start > end || end - start + 1 > Int.MAX_VALUE)) { "Illegal Argument" } return Random(System.nanoTime()).nextInt(end - start + 1) + start } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
9
6
5
9
5
This works as nextInt(end - start + 1) will generate a random number between 0 and (end - start) and adding start to it will give a random number between start and end.
The nextInt() function is overloaded to generate an integer random value uniformly distributed between the specified range.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import kotlin.random.Random fun rand(start: Int, end: Int): Int { require(start <= end) { "Illegal Argument" } return Random(System.nanoTime()).nextInt(start, end + 1) } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
8
5
6
8
6
3. Using Math.random() function
Another plausible way is to use the Math.random() function that returns a pseudorandom double value within the range [0.0, 1.0).
|
1 2 3 4 5 6 7 8 9 10 11 |
fun rand(start: Int, end: Int): Int { require(start <= end) { "Illegal Argument" } return (Math.random() * (end - start + 1)).toInt() + start } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
7
7
8
8
5
It works as Math.random() generates a random double in range of [0.0, 1.0). When multiplied by ((end - start) + 1), the lower limit remains 0, but the upper limit becomes (end - start, end - start + 1). On casting to an Int and adding start, the range becomes [start, end].
4. Using SecureRandom class
To get a cryptographically strong random number generator, consider using the SecureRandom class from the java.security package. Here’s a working example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.security.SecureRandom fun rand(start: Int, end: Int): Int { require(start <= end) { "Illegal Argument" } val random = SecureRandom() random.setSeed(random.generateSeed(20)) return random.nextInt(end - start + 1) + start } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
7
9
5
9
7
5. Using ThreadLocalRandom class
The recommended approach is to use the ThreadLocalRandom class to get better performance in multithreaded environments.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.concurrent.ThreadLocalRandom fun rand(start: Int, end: Int): Int { require(start <= end) { "Illegal Argument" } return ThreadLocalRandom.current().nextInt(start, end + 1) } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
6
7
9
6
6
6. Using shuffled() function
Finally, you can shuffle elements in the specified range and return the first or the last element after shuffling, which would be your random element.
|
1 2 3 4 5 6 7 8 9 10 11 |
fun rand(start: Int, end: Int): Int { require(start <= end) { "Illegal Argument" } return (start..end).shuffled().first() } fun main() { val start = 5 val end = 9 for (i in 1..5) println(rand(start, end)) } |
Output (will vary):
8
9
5
9
8
That’s all about generating random numbers between specified ranges in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)