What is an efficient way to get a random element from a collection in Scala? There's a related question here, but like one of the comments pointed out, "[that] question does not specify any efficiency needs".
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
- Unity - Get Random Color at Spawning
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- why 48 bit seed in util Random class?
- GRPC: make high-throughput client in Java/Scala
- Setting up multiple test folders in a SBT project
- Need help generating discrete random numbers from
If you need random order of all collection elements, then
Random.shuffle
is what you need. (You'd better convert the original collection to array to avoid forward and backward conversion.)Use a collection with a constant-time size() and get() method.
An arbitrary collection cannot be accessed in constant time. So you need some special collection with the desired property. For instance —
Vector
orArray
. See Performance Characteristics of collections for others.