在Scala中获取随机元素的有效方法? [英] Efficient way to get a random element in Scala?

查看:102
本文介绍了在Scala中获取随机元素的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Scala中的集合中获取随机元素的有效方法是什么?这里有一个相关的问题,但就像其中一项评论所指出的那样,"[该问题并未指定任何效率需求".

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

推荐答案

不能在恒定时间内访问任意集合.因此,您需要一些具有所需属性的特殊集合.例如-VectorArray.有关其他内容,请参见集合的性能特征.

An arbitrary collection cannot be accessed in constant time. So you need some special collection with the desired property. For instance — Vector or Array. See Performance Characteristics of collections for others.

这篇关于在Scala中获取随机元素的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆