nextDouble

open fun nextDouble(): Double(source)

Gets the next random Double value uniformly distributed between 0 (inclusive) and 1 (exclusive).

Samples



import kotlin.math.sin


import kotlin.random.Random


import kotlin.test.assertTrue
fun main() { 
   //sampleStart 
   if (Random.nextDouble() <= 0.3) {
    println("There was 30% possibility of rainy weather today and it is raining.")
} else {
    println("There was 70% possibility of sunny weather today and the sun is shining.")
} 
   //sampleEnd
}

open fun nextDouble(until: Double): Double(source)

Gets the next random non-negative Double from the random number generator less than the specified until bound.

Generates a Double random value uniformly distributed between 0 (inclusive) and until (exclusive).

Samples



import kotlin.math.sin


import kotlin.random.Random


import kotlin.test.assertTrue
fun main() { 
   //sampleStart 
   val firstAngle = Random.nextDouble(until = Math.PI / 6);
println("sin(firstAngle) < 0.5 is ${sin(firstAngle) < 0.5}") // true

val secondAngle = Random.nextDouble(from = Math.PI / 6, until = Math.PI / 2)
val sinValue = sin(secondAngle)
println("sinValue >= 0.5 && sinValue < 1.0 is ${sinValue >= 0.5 && sinValue < 1.0}") // true 
   //sampleEnd
}

Throws

if until is negative or zero.


open fun nextDouble(from: Double, until: Double): Double(source)

Gets the next random Double from the random number generator in the specified range.

Generates a Double random value uniformly distributed between the specified from (inclusive) and until (exclusive) bounds.

from and until must be finite otherwise the behavior is unspecified.

Samples



import kotlin.math.sin


import kotlin.random.Random


import kotlin.test.assertTrue
fun main() { 
   //sampleStart 
   val firstAngle = Random.nextDouble(until = Math.PI / 6);
println("sin(firstAngle) < 0.5 is ${sin(firstAngle) < 0.5}") // true

val secondAngle = Random.nextDouble(from = Math.PI / 6, until = Math.PI / 2)
val sinValue = sin(secondAngle)
println("sinValue >= 0.5 && sinValue < 1.0 is ${sinValue >= 0.5 && sinValue < 1.0}") // true 
   //sampleEnd
}

Throws

if from is greater than or equal to until.