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

sam picture sam · Feb 19, 2011 · Viewed 43.1k times · Source

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

Answer

user1338062 picture user1338062 · Feb 2, 2013
import scala.util.Random

val A = Array("please", "help", "me")
Random.shuffle(A.toList).head