What is an effective way to get a random item from a collection in Scala? There is a related question there, but, like one of the comments, βthe question does not ask for any need for efficiency.β
A custom collection may not be available for a fixed time. Therefore, you need a special collection with the desired property. For example - Vector or Array . See Collection Performance Specifications for others.
Vector
Array
Use a collection using the size () and get () methods with constant time.
If you need a random order of all elements of the collection, then Random.shuffle is what you need. (It is better to convert the original collection into an array to avoid direct and inverse conversion.)
Random.shuffle
util.Random.shuffle(List.range(1,100)) take 3
Source: https://habr.com/ru/post/1501081/More articles:https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1501076/android-studio-says-i-dont-have-android-support-repository-but-i-do&usg=ALkJrhhQ2BNCkIzlQdoWZJO2KUFDlbf5zgWhy can I call function templates without forward declarations? - c ++How to change the color of a button and pause for a few seconds when pressed? - androiddifference between input [type = hidden] and visibility = "hidden" - htmlAdd custom work to jobexecutor - javaHow to press Enter using Splinter? - pythonHow to make matplotlib: pyplot resizeable with a Tkinter window in Python? - pythonAngularJS: how to switch and replace containing element - angularjsgetWidth does not return another value after scaling - javaHow to install phabricator on Heroku? - herokuAll Articles