How to choose a random enumeration value

In Swift there is actually a protocol for enums called CaseIterable that, if you add it to your enum, you can just reference all of the cases as a collection with .allCases as so: enum GeometryClassification: CaseIterable { case Circle case Square case Triangle } and then you can .allCases and then .randomElement() to get … Read more

Go rand.Intn same number/value

2 reasons: You have to initalize the global Source used by rand.Intn() and other functions of the rand package using rand.Seed(). For example: rand.Seed(time.Now().UnixNano()) See possible duplicate of Difficulty with Go Rand package. Quoting from package doc of rand: Top-level functions, such as Float64 and Int, use a default shared Source that produces a deterministic … Read more

Get a random item from list using kotlin streams

Kotlin 1.3 and above: Kotlin 1.3 is now available with Multiplatform Random Number Generator! You can use it like this : import kotlin.random.Random fun main() { println(Random.nextBoolean()) println(Random.nextInt()) } Try it online! or in your case fun main() { val list = (1..9).filter { it % 2 == 0 } println(list.random()) } Try it online! … Read more