Logo

Programming-Idioms

History of Idiom 11 > diff from v29 to v30

Edit summary for version 30 by :
[Scala] More context

Version 29

2016-02-16, 21:50:22

Version 30

2016-02-16, 21:51:08

Idiom #11 Pick a random element from a list

List x must be non-empty.

Idiom #11 Pick a random element from a list

List x must be non-empty.

Imports
import scala.util.Random
Imports
import scala.util.Random
Code
val x = List(1, 2, 3, 4)
x(Random.nextInt(x.size))
Code
val x = List(1, 2, 3, 4)
x.apply(Random.nextInt(x.size))
// apply can be called without the name or dot
// x(Random.nextInt(x.size))