Skip to content

Instantly share code, notes, and snippets.

@LDuncAndroid
Created March 31, 2023 07:59
Show Gist options
  • Save LDuncAndroid/85abb9b5e463b2d7dcbbfec78bd6c2e6 to your computer and use it in GitHub Desktop.
Save LDuncAndroid/85abb9b5e463b2d7dcbbfec78bd6c2e6 to your computer and use it in GitHub Desktop.
import kotlin.random.Random
import kotlin.random.nextInt
// https://stackoverflow.com/posts/69076855/revisions @aSemy
val randomInts = generateSequence {
// this lambda is the source of the sequence's values
Random.nextInt(1..69)
}
// make the values distinct, so there's no repeated ints
.distinct()
// only fetch 6 values
// Note: It's very important that the source lambda can provide
// this many distinct values! If not, the stream will
// hang, endlessly waiting for more unique values.
.take(6)
// sort the values
.sorted()
// and collect them into a Set
.toSet()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment