randomOrNull

Returns a random LocalDate within the bounds of the LocalDateProgression or null if the progression is empty.

Takes the step into account; will not return any value within the range that would be skipped over by the progression.

Samples

import kotlinx.datetime.*
import kotlin.random.Random
import kotlin.test.Test

fun main() { 
   //sampleStart 
   // Getting a random element from a LocalDateProgression
check((LocalDate(2000, 1, 1)..LocalDate(2000, 1, 5)).random() in LocalDate(2000, 1, 1)..LocalDate(2000, 1, 5))
check((LocalDate(2000, 1, 1)..LocalDate(2000, 1, 5)).random(Random(123456)) in LocalDate(2000, 1, 1)..LocalDate(2000, 1, 5))
check((LocalDate(2000, 1, 5)..LocalDate(2000, 1, 1)).randomOrNull() == null)
check((LocalDate(2000, 1, 5)..LocalDate(2000, 1, 1)).randomOrNull(Random(123456)) == null) 
   //sampleEnd
}

Returns a random YearMonth within the bounds of the YearMonthProgression or null if the progression is empty.

Takes the step into account; will not return any value within the range that would be skipped over by the progression.

Samples

import kotlinx.datetime.*
import kotlin.random.Random
import kotlin.test.Test

fun main() { 
   //sampleStart 
   // Getting a random element from a YearMonthProgression
check((YearMonth(2000, 1)..YearMonth(2000, 5)).random() in YearMonth(2000, 1)..YearMonth(2000, 5))
check((YearMonth(2000, 1)..YearMonth(2000, 5)).random(Random(123456)) in YearMonth(2000, 1)..YearMonth(2000, 5))
check((YearMonth(2000, 5)..YearMonth(2000, 1)).randomOrNull() == null)
check((YearMonth(2000, 5)..YearMonth(2000, 1)).randomOrNull(Random(123456)) == null) 
   //sampleEnd
}