Scala ListBuffer (or equivalent) shuffle

前端 未结 1 650
醉话见心
醉话见心 2021-01-01 10:08

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

相关标签:
1条回答
  • 2021-01-01 10:25

    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...

    0 讨论(0)
提交回复
热议问题