 If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

## 1. Introduction

This short tutorial will demonstrate how to generate a random number using Kotlin.

## 2. Random Number Using java.lang.Math

The easiest way to generate a random number in Kotlin is to use java.lang.Math. Below example will generate a random double number between 0 and 1.

``````@Test
fun whenRandomNumberWithJavaUtilMath_thenResultIsBetween0And1() {
val randomNumber = Math.random()
assertTrue { randomNumber >= 0 }
assertTrue { randomNumber < 1 }
}``````

## 3. Random Number Using ThreadLocalRandom

We can also use java.util.concurrent.ThreadLocalRandom to generate a random double, integer or long value. Integer and long values generated this way can be both positive or negative.

``````@Test
assertTrue { randomDouble >= 0 }
assertTrue { randomDouble < 1 }
assertTrue { randomInteger >= Integer.MIN_VALUE }
assertTrue { randomInteger < Integer.MAX_VALUE }
assertTrue { randomLong >= Long.MIN_VALUE }
assertTrue { randomLong < Long.MAX_VALUE }
}``````

## 4. Random Number Using Kotlin.js

We can also generate a random double using the Math class from the kotlin.js library.

``````@Test
fun whenRandomNumberWithKotlinJSMath_thenResultIsBetween0And1() {
val randomDouble = Math.random()
assertTrue { randomDouble >=0 }
assertTrue { randomDouble < 1 }
}``````

## 5. Random Number in a Given Range Using Pure Kotlin

Using pure Kotlin, we can create a list of numbers, shuffle it, and then take the first element from that list:

``````@Test
fun whenRandomNumberWithKotlinNumberRange_thenResultInGivenRange() {
val randomInteger = (1..12).shuffled().first()
assertTrue { randomInteger >= 1 }
assertTrue { randomInteger <= 12 }
}``````

## 6. Random Number in a Given Range Using ThreadLocalRandom

ThreadLocalRandom presented in section 3 can also be used to generate a random number in a given range:

``````@Test
assertTrue { randomDouble >= 1 }
assertTrue { randomDouble < 10 }
assertTrue { randomInteger >= 1 }
assertTrue { randomInteger < 10 }
assertTrue { randomLong >= 1 }
assertTrue { randomLong < 10 }
}``````

## 7. Pseudo vs Secure Random Number Generators

Standard JDK implementations of java.util.Random use a Linear Congruential Generator to provide random numbers. The problem with this algorithm is that it’s not cryptographically strong, and its outputs could be predicted by attackers.

To overcome this issue, we should use java.security.SecureRandom in places where we need good security:

``````fun whenRandomNumberWithJavaSecureRandom_thenResultsInGivenRanges() {
val secureRandom = SecureRandom()
secureRandom.nextInt(100)
assertTrue { randomLong >= 0 }
assertTrue { randomLong < 100 }
}``````

SecureRandom produces cryptographically strong random values using a cryptographically strong pseudo-random number generator (CSPRNG).

Applications shouldn’t use other ways of generating a secure random number in places related to security.

## 8. Conclusion

In this article, we’ve learned a few ways to generate a random number in Kotlin.

As always, all code presented in this tutorial can be found over on GitHub.

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.