Kotlin Tutorials

Kotlin Set randomOrNull()
Syntax & Examples

Syntax of Set.randomOrNull()

There are 2 variations for the syntax of Set.randomOrNull() extension function. They are:

1.
fun <T> Collection<T>.randomOrNull(): T?

This extension function returns a random element from this collection, or null if this collection is empty.

2.
fun <T> Collection<T>.randomOrNull(random: Random): T?

This extension function returns a random element from this collection using the specified source of randomness, or null if this collection is empty.