This article explores different ways to generate a random double in Kotlin.

1. Using Random.nextDouble() function

The standard solution to generate a pseudorandom uniformly-distributed double value is using the Random.nextDouble() function. It generates a random value between 0.0 (inclusive) and 1.0 (exclusive) with a random number generator (RNG).

Download Code

 
To generate a pseudorandom uniformly-distributed double value in the specified range, we can do as follows:

Download Code

2. Using Random.nextFloat() function

The kotlin.random.Random class provides the corresponding functions to generate pseudorandom values for Integer, Long, Double, Float, Boolean, etc. To generate a pseudorandom float value between 0.0f (inclusive) and 1.0f (exclusive), we can call the Random.nextFloat() function.

Download Code

 
Additionally, we can extend the solution to work with the specified range:

Download Code

3. Using Math.random() function

Alternatively, we can use the Math.random() function to generate a pseudorandom double value. It returns a uniformly distributed value between 0.0 (inclusive) and 1.0 (exclusive).

Download Code

 
As with previous methods, we can easily extend the solution to any range:

Download Code

That’s all about generating a random double in Kotlin.