sam sam - 1 month ago 7
Scala Question

How to choose a random element from an array in Scala?

For example, there is a Scala array

val A = Array("please", "help", "me")
. How to choose a random element from this array?

Answer
import java.util.Random
// ...
val rand = new Random(System.currentTimeMillis())
val random_index = rand.nextInt(A.length)
val result = A(random_index)
Comments