Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala ListBuffer (or equivalent) shuffle

Is there a simple shuffle function for Scala lists?

If not, whats the simplest way to implement?

I have a lot of these things to do all over the code, so the simpler the call, the best it is

An example in Ruby

a = [ 1, 2, 3 ]           #=> [1, 2, 3] a.shuffle                 #=> [2, 3, 1] returns new array shuffled 

Thanks in advance :)

like image 409
rdlu Avatar asked Jun 14 '12 19:06

rdlu


1 Answers

In Scala you can use scala.util.Random:

util.Random.shuffle((1 to 10).toSeq) //Vector(9, 6, 8, 7, 10, 1, 2, 5, 3, 4)  util.Random.shuffle(List('A', 'B', 'C', 'D', 'E', 'F')) //List(B, D, A, E, C, F) 

Your results may vary...

like image 152
Tomasz Nurkiewicz Avatar answered Sep 29 '22 07:09

Tomasz Nurkiewicz