Scala ListBuffer(或等效)随机播放 [英] Scala ListBuffer (or equivalent) shuffle
本文介绍了Scala ListBuffer(或等效)随机播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Scala列表是否有简单的随机播放功能?
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
Ruby中的示例
a = [ 1, 2, 3 ] #=> [1, 2, 3]
a.shuffle #=> [2, 3, 1] returns new array shuffled
先谢谢您了:)
推荐答案
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...
这篇关于Scala ListBuffer(或等效)随机播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文