Pick random element

From Rosetta Code
Revision as of 22:31, 29 November 2021 by rosettacode>Amarok (Added solution for Action!)
Task
Pick random element
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate how to pick a random element from a list.

11l

Translation of: Python

<lang 11l>print(random:choice([‘foo’, ‘bar’, ‘baz’]))</lang>

8086 Assembly

The easiest way to pick a random element from a list is to use a random number generator's output as an index into an array. Care must be taken not to index out of bounds. If the array's size is a power of 2, then index = RNG output & (array size - 1) will ensure that the array is not indexed out of bounds while maintaining the randomness of the selection. Otherwise, you will have to manually check the RNG output against the array's size and roll again if the RNG output is larger.

For brevity's sake, the implementations of the RNG and printing routines were left out; they can be provided if requested. <lang asm> .model small

   .stack 1024
   .data

TestList byte 00h,05h,10h,15h,20h,25h,30h,35h

   .code

start: mov ax,@data mov ds,ax

mov ax,@code mov es,ax

call seedXorshift32 ;seeds the xorshift rng using the computer's date and time

call doXorshift32 mov ax,word ptr [ds:xorshift32_state_lo] ;retrieve the rng output and al,00000111b ;constrain the rng to values 0-7

mov bx,offset TestList XLAT ;translate AL according to [DS:BX]

call PrintHex ;display AL to the terminal


mov ax,4C00h int 21h ;exit program and return to MS-DOS

       end start</lang>


Output:

(After four runs of the program)

05
20
15
30

ACL2

<lang Lisp>:set-state-ok t

(defun pick-random-element (xs state)

  (mv-let (idx state)
          (random$ (len xs) state)
     (mv (nth idx xs) state)))</lang>

Action!

<lang Action!>PROC Main()

 DEFINE PTR="CARD"
 PTR ARRAY a(7)
 BYTE i,index
 a(0)="Monday"
 a(1)="Tuesday"
 a(2)="Wednesday"
 a(3)="Thursday"
 a(4)="Friday"
 a(5)="Saturday"
 a(6)="Sunday"
 FOR i=1 TO 20
 DO
   index=Rand(7)
   PrintE(a(index))
 OD

RETURN</lang>

Output:

Screenshot from Atari 8-bit computer

Thursday
Tuesday
Friday
Friday
Saturday
Tuesday
Monday
Saturday
Sunday
Wednesday
Monday
Friday
Wednesday
Saturday
Friday
Thursday
Saturday
Saturday
Wednesday
Wednesday

Ada

The following program generates three 20-letter words. Each vowel and each consonant is picked randomly from a list of vowels resp. a list of consonants.

<lang Ada>with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

  package Rnd renames Ada.Numerics.Float_Random;
  Gen: Rnd.Generator; -- used globally
  type Char_Arr is array (Natural range <>) of Character;
  function Pick_Random(A: Char_Arr) return Character is
     -- Chooses one of the characters of A (uniformly distributed)
  begin
     return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
  end Pick_Random;
  Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
  Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
  Specials  : Char_Arr := (',', '.', '?', '!');

begin

  Rnd.Reset(Gen);
  for J in 1 .. 3 loop
     for I in 1 .. 10 loop
        Ada.Text_IO.Put(Pick_Random(Consonants));
        Ada.Text_IO.Put(Pick_Random(Vowels));
     end loop;
     Ada.Text_IO.Put(Pick_Random(Specials) & " ");
  end loop;
  Ada.Text_IO.New_Line;

end Pick_Random_Element;</lang>

Output:
horanohesuhodinahiru. desehonirosedisinelo, losihehederidonolahe?

Aime

<lang aime>list l;

l_append(l, 'a'); l_append(l, 'b'); l_append(l, 'c'); l_append(l, 'd'); l_append(l, 'e'); l_append(l, 'f');

o_byte(l[drand(5)]); o_byte('\n');</lang>

ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.win32

<lang algol68># pick a random element from an array of strings #

OP PICKRANDOM = ( []STRING list )STRING: BEGIN

   INT number of elements = ( UPB list - LWB list ) + 1;
   INT random element     =
       ENTIER ( next random * ( number of elements ) );
   list[ LWB list + random element ]

END; # PICKRANDOM #

  1. can define additional operators for other types of array #


main: (

   []STRING days = ( "Sunday",   "Monday", "Tuesday", "Wednesday"
                   , "Thursday", "Friday", "Saturday"
                   );
   print( ( PICKRANDOM days, newline ) )

)</lang>

Output:
Thursday

APL

Works with: Dyalog APL

<lang apl>pickRandom ← (?≢)⊃⊢</lang>

Output:
      pickRandom 'ABCDE'
E
      pickRandom 'ABCDE'
D
      pickRandom 'ABCDE'
A

App Inventor

App Inventor has the block pick a random item for selecting a random item from a list.
CLICK HERE TO VIEW THE BLOCKS AND ANDROID APP DISPLAY

AppleScript

<lang AppleScript>get some item of [1, "two", pi, "4", 5 > 4, 5 + 1, Sunday]</lang>

Output:
"two"

Arturo

<lang rebol>fruit: ["apple" "banana" "pineapple" "apricot" "watermelon"]

print sample fruit</lang>

Output:
pineapple

AutoHotkey

True Arrays
Works with: AutoHotkey_L

<lang AHK>list := ["abc", "def", "gh", "ijklmnop", "hello", "world"] Random, randint, 1, % list.MaxIndex() MsgBox % List[randint]</lang>

Pseudo-Arrays
Works with: AutoHotkey_Basic

<lang AutoHotkey>list := "abc,def,gh,ijklmnop,hello,world" StringSplit list, list, `, Random, randint, 1, %list0% MsgBox % List%randint%</lang>

AWK

<lang AWK># syntax: GAWK -f PICK_RANDOM_ELEMENT.AWK BEGIN {

   n = split("Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday",day_of_week,",")
   srand()
   x = int(n*rand()) + 1
   printf("%s\n",day_of_week[x])
   exit(0)

}</lang>

Output:
GAWK -f PICK_RANDOM_ELEMENT.AWK
Sunday
GAWK -f PICK_RANDOM_ELEMENT.AWK
Monday
GAWK -f PICK_RANDOM_ELEMENT.AWK
Wednesday
GAWK -f PICK_RANDOM_ELEMENT.AWK
Tuesday

BaCon

This is simply an application of a ranged random number used as an array index. BaCon has no built in random element selector.

<lang freebasic>' Pick random element OPTION BASE 1 DECLARE words$[6] FOR i = 1 TO 6 : READ words$[i] : NEXT DATA "Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta"

element = RANDOM(6) + 1 PRINT "Chose ", element, ": ", words$[element]</lang>

Output:
prompt$ ./pick-random-element
Chose 2: Beta
prompt$ ./pick-random-element
Chose 1: Alpha
prompt$ ./pick-random-element
Chose 5: Epsilon

Bash

<lang Bash># borrowed from github.com/search?q=bashnative

rand() { printf $(( $1 * RANDOM / 32767 )) } rand_element () {

   local -a th=("$@")
   unset th[0]
   printf $'%s\n' "${th[$(($(rand "${#th[*]}")+1))]}"

}

echo "You feel like a $(rand_element pig donkey unicorn eagle) today"</lang>

BASIC

Works with: QBasic
Works with: PowerBASIC

Note the use of LBOUND and UBOUND. This is only necessary for arrays where the lower and upper limits aren't known. In this example, we know they are 0 and 10 respectively, and could have hard-coded those numbers. (For that matter, the "random selection" line could've just been entered as x = INT(RND * 11).)

<lang qbasic>'setup DIM foo(10) AS LONG DIM n AS LONG, x AS LONG FOR n = LBOUND(foo) TO UBOUND(foo)

   foo(n) = INT(RND*99999)

NEXT RANDOMIZE TIMER

'random selection x = INT(RND * ((UBOUND(foo) - LBOUND(foo)) + 1))

'output PRINT x, foo(x)</lang>

See also: Liberty BASIC, PureBasic, Run BASIC

Commodore BASIC

<lang qbasic>10 DIM A$(9) 20 FOR I=0 TO 9 : READ A$(I) : NEXT 30 X = RND(-TI) : REM 'PLANT A RANDOM SEED' 40 X = INT(RND(1)*10) 50 PRINT A$(X) 60 END 100 DATA ALPHA, BRAVO, CHARLIE, DELTA, ECHO 110 DATA FOXTROT, GOLF, HOTEL, INDIA, JULIETT</lang>

Batch File

Since there is no arrays in Batch File, I will use a 1-based pseudo-array. <lang dos>@echo off setlocal enabledelayedexpansion

::Initializing the pseudo-array... set "pseudo=Alpha Beta Gamma Delta Epsilon" set cnt=0 & for %%P in (!pseudo!) do ( set /a cnt+=1 set "pseudo[!cnt!]=%%P" ) ::Do the random thing... set /a rndInt=%random% %% cnt +1

::Print the element corresponding to rndint... echo.!pseudo[%rndInt%]! pause exit /b</lang>

Sample Outputs:
Delta
Press any key to continue . . .

Gamma
Press any key to continue . . .

Epsilon
Press any key to continue . . .

Gamma
Press any key to continue . . .

BBC BASIC

<lang bbcbasic> DIM list$(5)

     list$() = "The", "five", "boxing", "wizards", "jump", "quickly"
     chosen% = RND(6)
     PRINT "Item " ; chosen% " was chosen which is '" list$(chosen%-1) "'"</lang>
Output:
Item 4 was chosen which is 'wizards'

Burlesque

<lang burlesque> blsq ) "ABCDEFG"123456 0 6rn-]!! 'G </lang>

123456 is the random seed. In order to pick another element you have to change the random seed.

C

<lang c>

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <time.h>

int main(){

  char array[] = { 'a', 'b', 'c','d','e','f','g','h','i','j' };
  int i;
  time_t t;
  srand((unsigned)time(&t));
  
  for(i=0;i<30;i++){

printf("%c\n", array[rand()%10]);

  }
  
  return 0;

} </lang> Output

a
e
f
h
b
d
g
a
b
f
a
i
b
d
d
g
j
a
f
e
a
e
g
e
i
d
j
a
f
e
a

C#

<lang csharp>using System; using System.Collections.Generic;

class RandomElementPicker {

 static void Main() {
   var list = new List<int>(new[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
   var rng = new Random();
   var randomElement = list[rng.Next(list.Count)];
   Console.WriteLine("I picked element {0}", randomElement);
 }

}</lang>

C++

<lang cpp>#include <iostream>

  1. include <random>
  2. include <vector>

int main( ) {

  std::vector<int> numbers { 11 , 88 , -5 , 13 , 4 , 121 , 77 , 2 } ;
  std::random_device seed ;
  // generator 
  std::mt19937 engine( seed( ) ) ;
  // number distribution
  std::uniform_int_distribution<int> choose( 0 , numbers.size( ) - 1 ) ;
  std::cout << "random element picked : " << numbers[ choose( engine ) ] 
     << " !\n" ;
  return 0 ;

}</lang>

Ceylon

<lang ceylon>import ceylon.random {

DefaultRandom }

shared void run() {

   value random = DefaultRandom();
   value element = random.nextElement([1, 2, 3, 4, 5, 6]);
   print(element);

}</lang>

Clojure

<lang Clojure>(rand-nth coll)</lang>

where coll is some sequential collection. Equivalent to:

<lang Clojure>(nth coll (rand-int (count coll)))</lang>

COBOL

Works with: GNU Cobol

<lang cobol> >>SOURCE FREE IDENTIFICATION DIVISION. PROGRAM-ID. random-element.

DATA DIVISION. WORKING-STORAGE SECTION. 01 nums-area VALUE "123456789".

   03  nums                            PIC 9 OCCURS 9 TIMES.
   

01 random-idx PIC 9 COMP.

PROCEDURE DIVISION.

   COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
   DISPLAY nums (random-idx)
   .

END PROGRAM random-element.</lang>

CoffeeScript

<lang coffeescript>array = [1,2,3] console.log array[Math.floor(Math.random() * array.length)]</lang>

Common Lisp

<lang lisp>(defvar *list* '(one two three four five))

(print (nth (random (length *list*)) *list*)) (print (nth (random (length *list*)) *list*)) (print (nth (random (length *list*)) *list*))</lang>

Output:
FIVE 
THREE 
ONE

Crystal

<lang Ruby> puts [1, 2, 3, 4, 5].sample(1) </lang>

D

<lang d>import std.stdio, std.random;

void main() {

   const items = ["foo", "bar", "baz"];
   items[uniform(0, $)].writeln;

}</lang>

Delphi

See #Pascal / Delphi / Free Pascal.

Déjà Vu

<lang dejavu>!print choose [ "one" "two" "chicken" ]</lang>

EasyLang

<lang easyprog.online>ar$[] = [ "spring" "summer" "autumn" "winter" ] print ar$[random len ar$[]]</lang>

EchoLisp

<lang lisp> (define (pick-random list)

   (list-ref list (random (length list))))

(pick-random (iota 1000)) → 667 (pick-random (iota 1000)) → 179 </lang>

Elena

ELENA 5.0 : <lang elena>import extensions;

extension listOp {

   randomItem()
       = self[randomGenerator.eval(self.Length)];

}

public program() {

   var item := new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

   console.printLine("I picked element ",item.randomItem())

}</lang>

Elixir

Works with: Elixir version 1.2

<lang elixir>iex(1)> list = Enum.to_list(1..20) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] iex(2)> Enum.random(list) 19 iex(3)> Enum.take_random(list,4) [19, 20, 7, 15]</lang>

Emacs Lisp

<lang Lisp> (defun random-choice (items)

 (let* ((size (length items))
        (index (random size)))
   (nth index items)))

(random-choice '("a" "b" "c"))

=> "a"

</lang>

Erlang

<lang erlang>% Implemented by Arjun Sunel -module(pick_random). -export([main/0]).

main() -> List =[1,2,3,4,5], Index = rand:uniform(length(List)), lists:nth(Index,List). </lang>

Euphoria

<lang euphoria>constant s = {'a', 'b', 'c'} puts(1,s[rand($)])</lang>

F#

<lang fsharp>let list = ["a"; "b"; "c"; "d"; "e"] let rand = new System.Random() printfn "%s" list.[rand.Next(list.Length)]</lang>

Factor

<lang factor>( scratchpad ) { "a" "b" "c" "d" "e" "f" } random . "a"</lang>

Falcon

<lang falcon> lst = [1, 3, 5, 8, 10] > randomPick(lst) </lang>

Fortran

Works with: Fortran version 90 and later

<lang fortran>program pick_random

 implicit none
 integer :: i
 integer :: a(10) = (/ (i, i = 1, 10) /)
 real :: r
 call random_seed
 call random_number(r)
 write(*,*) a(int(r*size(a)) + 1)

end program</lang>

Free Pascal

See #Pascal / Delphi / Free Pascal.

FreeBASIC

<lang freebasic>' FB 1.05.0 Win64

Dim a(0 To 9) As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}

Randomize Dim randInt As Integer

For i As Integer = 1 To 5

 randInt = Int(Rnd * 10)
 Print a(randInt)

Next Sleep</lang>

Sample output :

Output:
Zero
Seven
Three
Nine
Three

Frink

<lang frink>a = ["one", "two", "three"] println[random[a]]</lang>

Gambas

You can run this code. Copy the code, click this link, paste it in and press 'Run !' <lang gambas>Public Sub Main() Dim sList As String[] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

Print sList[Rand(0, 11)]

End </lang> Output:

May

GAP

<lang gap>a := [2, 9, 4, 7, 5, 3]; Random(a);</lang>

This works with many GAP objects, for instance groups:

<lang gap>Random(SymmetricGroup(20));

(1,4,8,2)(3,12)(5,14,10,18,17,7,16)(9,13)(11,15,20,19)</lang>

Go

<lang go>package main

import (

   "fmt"
   "math/rand"
   "time"

)

var list = []string{"bleen", "fuligin", "garrow", "grue", "hooloovoo"}

func main() {

   rand.Seed(time.Now().UnixNano())
   fmt.Println(list[rand.Intn(len(list))])

}</lang>

Groovy

Solution: <lang groovy>def list = [25, 30, 1, 450, 3, 78] def random = new Random();

(0..3).each {

   def i = random.nextInt(list.size())
   println "list[${i}] == ${list[i]}"

}</lang>

Output:
list[3] == 450
list[2] == 1
list[5] == 78
list[3] == 450

Alternate Solution: <lang groovy> [25, 30, 1, 450, 3, 78].sort{new Random()}?.take(1)[0] </lang>

GW-BASIC

<lang gwbasic>10 RANDOMIZE TIMER  : REM set random number seed to something arbitrary 20 DIM ARR(10)  : REM initialise array 30 FOR I = 1 TO 10 40 ARR(I) = I*I  : REM squares of the integers is OK as a demo 50 NEXT I 60 C = 1 + INT(RND*10)  : REM get a random index from 1 to 10 inclusive 70 PRINT ARR(C)</lang>

Output:
 81

Haskell

Creating a custom function:

<lang haskell>import System.Random (randomRIO)

pick :: [a] -> IO a pick xs = fmap (xs !!) $ randomRIO (0, length xs - 1)

x <- pick [1, 2, 3]</lang>

Using the random-fu library:

<lang haskell>import Data.Random sample $ randomElement [1, 2, 3]</lang>

For example: <lang haskell>do

 x <- sample $ randomElement  [1, 2, 3]
 print x</lang>

Icon and Unicon

The unary operator '?' selects a random element from its argument which may be a string, list, table, or set.

<lang Icon>procedure main()

  L := [1,2,3]  # a list
  x := ?L       # random element

end</lang>

J

<lang j> ({~ ?@#) 'abcdef' b</lang>

Java

<lang java>import java.util.Random; ... int[] array = {1,2,3}; return array[new Random().nextInt(array.length)]; // if done multiple times, the Random object should be re-used</lang>

For a List object rather than an array, substitute list.get(...) for array[...]. If preserving the order of the List isn't important, you could call Collections.shuffle(list); and then list.get(0);. You would need to shuffle each time unless you removed the item from the list.

JavaScript

<lang javascript>var array = [1,2,3]; return array[Math.floor(Math.random() * array.length)];</lang>

jq

Works with: jq

Works with gojq, the Go implementation of jq

Neither jq nor gojq currently has a built-in PRNG, but it is quite straightforward to define one if an external source of entropy is available. In this entry, `/dev/urandom` is used like so: <lang sh>< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -MRcnr -f program.jq</lang>

<lang jq># Output: a prn in range(0;$n) where $n is `.` def prn:

 if . == 1 then 0
 else . as $n
 | ([1, (($n-1)|tostring|length)]|max) as $w
 | [limit($w; inputs)] | join("") | tonumber
 | if . < $n then . else ($n | prn) end
 end;
  1. An illustration - 10 selections at random with replacement:

range(0;10) | ["a", "b", "c"] | .[length|prn]</lang>

Output:
bcbabacbbc

Julia

<lang julia>array = [1,2,3] rand(array)</lang>

K

<lang K> 1?"abcdefg" ,"e"</lang>

Klingphix

<lang Klingphix>include ..\Utilitys.tlhy

pickran len rand * 1 + get ;

( 1 3.1415 "Hello world" ( "nest" "list" ) )

10 [drop pickran ?] for

" " input</lang>

Output:
("nest", "list")
3.1415
("nest", "list")
Hello world
1
Hello world
3.1415
("nest", "list")
1
3.1415

Kotlin

<lang scala>// version 1.2.10

import java.util.Random

/**

* Extension function on any list that will return a random element from index 0 
* to the last index
*/

fun <E> List<E>.getRandomElement() = this[Random().nextInt(this.size)]

/**

* Extension function on any list that will return a list of unique random picks
* from the list. If the specified number of elements you want is larger than the
* number of elements in the list it returns null
*/

fun <E> List<E>.getRandomElements(numberOfElements: Int): List<E>? {

   if (numberOfElements > this.size) {
       return null
   }
   return this.shuffled().take(numberOfElements)

}

fun main(args: Array<String>) {

   val list = listOf(1, 16, 3, 7, 17, 24, 34, 23, 11, 2)
   println("The list consists of the following numbers:\n${list}")

   // notice we can call our extension functions as if they were regular member functions of List
   println("\nA randomly selected element from the list is ${list.getRandomElement()}")
   println("\nA random sequence of 5 elements from the list is ${list.getRandomElements(5)}")

}</lang>

Sample output:

Output:
The list consists of the following numbers:
[1, 16, 3, 7, 17, 24, 34, 23, 11, 2]

A randomly selected element from the list is 11

A random sequence of 5 elements from the list is [17, 24, 23, 16, 3]

LabVIEW

This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

Lasso

<lang Lasso>local( my array = array('one', 'two', 3) )

  1. myarray -> get(integer_random(#myarray -> size, 1))</lang>

-> two

Liberty BASIC

The natural way to hold an array of text is in a space- or comma-delimited string, although an array could be used. <lang lb>list$ ="John Paul George Ringo Peter Paul Mary Obama Putin" wantedTerm =int( 10 *rnd( 1)) print "Selecting term "; wantedTerm; " in the list, which was "; word$( list$, wantedTerm, " ")</lang>

Selecting term 5 in the list, which was Peter

LiveCode

<lang LiveCode>put "Apple,Banana,Peach,Apricot,Pear" into fruits put item (random(the number of items of fruits)) of fruits</lang>

Works with: UCB Logo

<lang logo>pick [1 2 3]</lang>

Lua

<lang lua>math.randomseed(os.time()) local a = {1,2,3} print(a[math.random(1,#a)])</lang>

Maple

<lang maple>a := [bear, giraffe, dog, rabbit, koala, lion, fox, deer, pony]: randomNum := rand(1 ..numelems(a)): a[randomNum()];</lang>

Mathematica/Wolfram Language

<lang Mathematica>RandomChoice[{a, b, c}]</lang>

Output:
c

MATLAB / Octave

In case list is a cell array: <lang Matlab> list = {'a','b','c'}; list{ceil(rand(1)*length(list))}</lang>

If list is a vector: <lang Matlab> list = 1:1000; list(ceil(rand(1)*length(list)))</lang>

Maxima

<lang Maxima>random_element(l):= part(l, 1+random(length(l))); /* (%i1) random_element(['a, 'b, 'c]);

   (%o1)                                  c
  • /</lang>

МК-61/52

<lang>0 П0 1 П1 2 П2 3 П3 4 П4 5

^ СЧ * [x] ПE КИПE С/П</lang>

Nanoquery

<lang Nanoquery>import Nanoquery.Util list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} println list[new(Random).getInt(len(list))]</lang>

Output:
12

<lang NetLogo>;; from list containnig only literals and literal constants user-message one-of [ 1 3 "rooster" blue ]

from list containing variables and reporters

user-message one-of (list (red + 2) turtles (patch 0 0) )</lang>

NetRexx

<lang netrexx>/* NetRexx */ options replace format comments java crossref savelog symbols nobinary

iArray = [ 1, 2, 3, 4, 5 ] -- a traditional array iList = Arrays.asList(iArray) -- a Java Collection "List" object iWords = '1 2 3 4 5' -- a list as a string of space delimited words


v1 = iArray[Random().nextInt(iArray.length)] v2 = iList.get(Random().nextInt(iList.size())) v3 = iWords.word(Random().nextInt(iWords.words()) + 1) -- the index for word() starts at one

say v1 v2 v3 </lang>

NewLISP

<lang NewLISP> (define (pick-random-element R) (nth (rand (length R)) R)) </lang> Example:

(setq X '("alpha" "beta" "gamma" "delta" "epsilon"))
(println (pick-random-element X))
(println (pick-random-element X))
(println (pick-random-element X))
(println (pick-random-element X))

Nim

<lang nim>import random randomize()

let ls = @["foo", "bar", "baz"] echo sample(ls)</lang>

Objeck

<lang objeck>values := [1, 2, 3]; value := values[(Float->Random() * 100.0)->As(Int) % values->Size()];</lang>

OCaml

With a list: <lang ocaml>let list_rand lst =

 let len = List.length lst in
 List.nth lst (Random.int len)</lang>
# list_rand [1;2;3;4;5] ;;
- : int = 3

With an array: <lang ocaml>let array_rand ary =

 let len = Array.length ary in
 ary.(Random.int len)</lang>
# array_rand [|1;2;3;4;5|] ;;
- : int = 3

Oforth

<lang Oforth>: pickRand(l) l size rand l at ;</lang>

Ol

<lang scheme> (import (otus random!))

(define x '("Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday")) (print (list-ref x (rand! (length x)))) </lang>

PARI/GP

<lang parigp>pick(v)=v[random(#v)+1]</lang>

Pascal / Delphi / Free Pascal

<lang pascal>Program PickRandomElement (output);

const

 s: array [1..5] of string = ('1234', 'ABCDE', 'Charlie', 'XB56ds', 'lala');

begin

 randomize;
 writeln(s[low(s) + random(length(s))]);

end.</lang>

Perl

<lang perl>my @array = qw(a b c); print $array[ rand @array ];</lang>

Phix

<lang Phix>constant s = {'a','b','c'} puts(1,s[rand(length(s))])</lang>

PHP

<lang php>$arr = array('foo', 'bar', 'baz'); $x = $arr[array_rand($arr)];</lang>

PicoLisp

<lang PicoLisp>(get Lst (rand 1 (length Lst)))</lang>

PL/I

<lang pli> declare t(0:9) character (1) static initial

     ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
  put ( t(10*random()) );</lang>
Output:
e

Powershell

Powershell has Get-Random Cmdlet which one of its overload is to select randomly from a given list

<lang Powershell> 1..100 | Get-Random -Count 3 </lang>

Prolog

Works with: SWI-Prolog version 6

<lang prolog> ?- random_member(M, [a, b, c, d, e, f, g, h, i, j]). M = i. </lang>

PureBasic

<lang PureBasic>Procedure.s pickRandomElement(List source.s())

 Protected x = ListSize(source())
 
 If x > 0
   SelectElement(source(), Random(x - 1)) ;element numbering is zero - based
   ProcedureReturn source()
 EndIf

EndProcedure

initialize list elements

DataSection

 elements:
 Data.s "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"

EndDataSection

  1. elementCount = 10

NewList item.s()

Restore elements Define i For i = 1 To #elementCount

 AddElement(item())
 Read.s item()

Next

If OpenConsole()

 Print("Source list:  ")
 ForEach item()
   Print(item() + " ")
 Next
 PrintN(#CRLF$)
  
 Print("Random picks from list:  ")
 For i = 1 To 10
   Print(pickRandomElement(item()) + " ")
 Next
  
 Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
 CloseConsole()

EndIf</lang>

Output:
Source list:  One Two Three Four Five Six Seven Eight Nine Ten

Random picks from list:  Seven Nine Two Six Four Four Nine Three Six Two

Easy version

<lang purebasic>OpenConsole()

a$="One" +#TAB$+ "Two" +#TAB$+ "Three" +#TAB$+ "Four" +#TAB$+ "Five" +#TAB$+

  "Six" +#TAB$+ "Seven"+#TAB$+ "Eight" +#TAB$+ "Nine" +#TAB$+ "Ten"  +#TAB$

Print("Source list: "+#TAB$+a$+#CRLF$+"Random list: "+#TAB$)

For i=1 To CountString(a$,#TAB$)

 Print(StringField(a$,Random(CountString(a$,#TAB$),1),#TAB$)+#TAB$)

Next Input()</lang>

Output:
Source list:    One     Two     Three   Four    Five    Six     Seven   Eight   Nine    Ten
Random list:    One     Two     Seven   Nine    Ten     Seven   Three   Five    Ten     Nine

Python

<lang python>>>> import random >>> random.choice(['foo', 'bar', 'baz']) 'baz'</lang>

Quackery

<lang quackery> [ dup size random peek ] is pick ( [ --> x )

 randomise
 ' [ 20 33 -15 7 0 ] pick echo cr
 ' pick pick echo</lang> 
Output:
33
random

R

<lang rsplus># a vector (letters are builtin) letters

  1. [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
  2. [20] "t" "u" "v" "w" "x" "y" "z"
  1. picking one element

sample(letters, 1)

  1. [1] "n"
  1. picking some elements with repetition, and concatenating to get a word

paste(sample(letters, 10, rep=T), collapse="")

  1. [1] "episxgcgmt"</lang>

Racket

<lang Racket>

  1. lang racket

(define (pick-item l)

 (list-ref l (random (length l))))

</lang>

Raku

(formerly Perl 6)

Works with: rakudo version 2015-12-07

In a nutshell, picking an element from a list is implemented with a method conveniently called "pick": <lang perl6>say (1, 2, 3).pick;</lang>

There are various ways of doing something similar, though. Raku has actually two methods (with associated functional forms) to return random elements depending on whether you are doing selection with or without replacement.

Selection with replacement: (roll of a die) <lang perl6>say (1..6).roll; # return 1 random value in the range 1 through 6 say (1..6).roll(3); # return a list of 3 random values in the range 1 through 6 say (1..6).roll(*)[^100]; # return first 100 values from a lazy infinite list of random values in the range 1 through 6</lang>

Selection without replacement: (pick a card from a deck) <lang perl6># define the deck my @deck = <2 3 4 5 6 7 8 9 J Q K A> X~ <♠ ♣ ♥ ♦>; say @deck.pick; # Pick a card say @deck.pick(5); # Draw 5 say @deck.pick(*); # Get a shuffled deck</lang> Or you can always use the normal rand built-in to generate a subscript (which automatically truncates any fractional part): <lang perl6>@array[@array * rand]</lang> However, the pick and roll methods (not to be confused with the pick-and-roll method in basketball) are more general insofar as they may be used on any enumerable type: <lang perl6>say Bool.pick; # returns either True or False</lang>

Red

<lang Red>>> random/only collect [repeat i 10 [keep i]]</lang>

ReScript

<lang ReScript>let fruits = ["apple", "banana", "coconut", "orange", "lychee"]

let pickRand = arr => {

 let len = Js.Array.length(arr)
 let i = Js.Math.random_int(0, len)
 arr[i]

}

Js.log(pickRand(fruits))</lang>

Output:
$ bsc pickrand.res > pickrand.bs.js
$ node pickrand.bs.js
lychee

REXX

version 1

This REXX example takes the Rosetta Code task very literally.

Also, the newly named elements have been incorporated into this table. <lang rexx>/*REXX program picks a random element from a list (tongue in cheek, a visual pun).*/ _= 'hydrogen helium lithium beryllium boron carbon nitrogen oxygen fluorine neon sodium' _=_ 'magnesium aluminum silicon phosphorous sulfur chlorine argon potassium calcium' _=_ 'scandium titanium vanadium chromium manganese iron cobalt nickel copper zinc gallium' _=_ 'germanium arsenic selenium bromine krypton rubidium strontium yttrium zirconium' _=_ 'niobium molybdenum technetium ruthenium rhodium palladium silver cadmium indium tin' _=_ 'antimony tellurium iodine xenon cesium barium lanthanum cerium praseodymium' _=_ 'neodymium promethium samarium europium gadolinium terbium dysprosium holmium erbium' _=_ 'thulium ytterbium lutetium hafnium tantalum tungsten rhenium osmium iridium platinum' _=_ 'gold mercury thallium lead bismuth polonium astatine radon francium radium actinium' _=_ 'thorium protactinium uranium neptunium plutonium americium curium berkelium' _=_ 'californium einsteinium fermium mendelevium nobelium lawrencium rutherfordium dubnium' _=_ 'seaborgium bohrium hassium meitnerium darmstadtium roentgenium copernicium nihonium' _=_ 'flerovium moscovium livermorium tennessine oganesson ununenniym unbinvlium umbiunium'

                                                /*─────  You can't trust atoms,   ─────*/
                                                /*─────  they make everything up. ─────*/
  1. = words(_) /*obtain the number of words in list. */

item= word(_, random(1, #) ) /*pick a random word (element) in list.*/ say 'random element: ' item /*stick a fork in it, we're all done. */</lang>

output:
random element:  tennessine

version 2

Slightly simplified:

Note that this version doesn't work (receives a syntax error 12) with REXXes that have a
smaller limit of the total length of a clause, in particular PC/REXX and Personal REXX
which have a limit of 1,000 characters). <lang rexx> /* REXX ***************************************************************

  • 18.10.2012 Walter Pachl Not only the list of elements shortened:-)
                                                                                                                                            • /

wl='hydrogen helium lithium beryllium boron carbon nitrogen oxygen',

  'fluorine neon sodium magnesium aluminum silicon phosphorous sulfur',   
  '...',                                                                  
  'meitnerium darmstadtium roentgenium copernicium Ununtrium'             
                                                                       

Say word(wl,random(1,words(wl))) </lang>

Ring

<lang ring> aList = "abcdefghij" for i = 1 to 10

   letter = random(9) + 1
   if letter > 0
      see aList[letter] + nl
   ok       

next </lang>

Ruby

<lang ruby> %w(north east south west).sample # => "west" (1..100).to_a.sample(2) # => [17, 79]</lang>

Run BASIC

<lang runbasic>list$ = "a,b,c,d,e,f,g,h,i,j" letter = rnd(1) * 10 print "Selected letter:"; word$(list$,letter,",")</lang>

Rust

Library: rand

<lang rust>extern crate rand;

use rand::Rng;

fn main() {

   let array = [5,1,2,5,6,7,8,1,2,4,5];
   let mut rng = rand::thread_rng();
   
   println!("{}", rng.choose(&array).unwrap());

}</lang>

Scala

Library: Scala

<lang Scala>val a = (1 to 10).toList

println(scala.util.Random.shuffle(a).head)</lang>

Seed7

<lang seed7>$ include "seed7_05.s7i";

const proc: main is func

 begin
   writeln(rand([] ("foo", "bar", "baz")));
 end func;</lang>

Sidef

<lang ruby>var arr = %w(north east south west); say arr.rand; say arr.rand(2).dump;</lang>

Output:
south
['west', 'south']

Smalltalk

<lang smalltalk>x := #(1 2 3) atRandom.</lang>

SuperCollider

<lang SuperCollider>[1, 2, 3].choose</lang>

Swift

<lang Swift>import Darwin

let myList = [1, 2, 4, 5, 62, 234, 1, -1] print(myList[Int(arc4random_uniform(UInt32(myList.count)))])</lang>

Tcl

Random selection from a list is implemented by composing lindex (for selection of an item from a list) and the pattern for generating an integral random number from the range . It's simpler to use when wrapped up as a helper procedure: <lang tcl>proc randelem {list} {

   lindex $list [expr {int(rand()*[llength $list])}]

} set x [randelem {1 2 3 4 5}]</lang>

TUSCRIPT

<lang tuscript>$$ MODE TUSCRIPT list="John'Paul'George'Ringo'Peter'Paul'Mary'Obama'Putin" sizeList=SIZE(list) selectedNr=RANDOM_NUMBERS (1,sizeList,1) selectedItem=SELECT(list,#selectednr) PRINT "Selecting term ",selectedNr," in the list, which was ",selectedItem</lang>

Output:
Selecting term 3  in the list, which was George

TXR

Translation of: Tcl

<lang txr>@(do (defun randelem (seq)

      [seq (random nil (length seq))]))

@(bind x @(randelem #("a" "b" "c" "d")))</lang>

UNIX Shell

Works with: Bourne Again Shell

<lang sh>list=(these are some words) printf '%s\n' "${list[RANDOM%${#list[@]}]}"</lang>

Works with: Zsh

<lang sh>list=(these are some words) printf '%s\n' "$list[RANDOM%$#list+1]"</lang>


Ursa

<lang ursa># generate a stream (ursa equivalent of a list) decl string<> str append "these" "are" "some" "values" str

decl ursa.util.random r out str<(r.getint (size str))> endl console</lang>

VBA

<lang vb> Option Explicit

Sub Main_Pick_Random_Element()

   Debug.Print Pick_Random_Element(Array(1, 2, 3, 4, 5, #11/24/2017#, "azerty"))

End Sub

Function Pick_Random_Element(myArray)

   Randomize Timer
   Pick_Random_Element = myArray(Int((Rnd * (UBound(myArray) - LBound(myArray) + 1) + LBound(myArray))))

End Function </lang>

VBScript

<lang vb>Function pick_random(arr) Set objRandom = CreateObject("System.Random") pick_random = arr(objRandom.Next_2(0,UBound(arr)+1)) End Function

WScript.Echo pick_random(Array("a","b","c","d","e","f"))</lang>

Output:
d

Visual Basic .NET

Translation of: C#

<lang vbnet>Module Program

   Sub Main()
       Dim list As New List(Of Integer)({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
       Dim rng As New Random()
       Dim randomElement = list(rng.Next(list.Count)) ' Upper bound is exclusive.
       Console.WriteLine("I picked element {0}", randomElement)
   End Sub

End Module</lang>

Wren

<lang ecmascript>import "random" for Random

var rand = Random.new() var colors = ["red", "green", "blue", "yellow", "pink"] for (i in 0..4) System.print(colors[rand.int(colors.count)])</lang>

Output:

Sample run:

blue
red
pink
yellow
red

XBS

<lang XBS>set Array = ["Hello","World",1,2,3]; log(Array[rnd(0,?Array-1)]);</lang>

XPL0

<lang XPL0>code Ran=1, Text=12; int List; [List:= ["hydrogen", "helium", "lithium", "beryllium", "boron"]; \(Thanks REXX) Text(0, List(Ran(5))); ]</lang>

Zig

This example uses the PCG algorithm.

<lang zig>const std = @import("std");

const debug = std.debug; const rand = std.rand; const time = std.time;

test "pick random element" {

   var pcg = rand.Pcg.init(time.milliTimestamp());
   const chars = [_]u8{
       'A', 'B', 'C', 'D',
       'E', 'F', 'G', 'H',
       'I', 'J', 'K', 'L',
       'M', 'N', 'O', 'P',
       'Q', 'R', 'S', 'T',
       'U', 'V', 'W', 'X',
       'Y', 'Z', '?', '!',
       '<', '>', '(', ')',
   };
   var i: usize = 0;
   while (i < 32) : (i += 1) {
       if (i % 4 == 0) {
           debug.warn("\n  ", .{});
       }
       debug.warn("'{c}', ", .{chars[pcg.random.int(usize) % chars.len]});
   }
   debug.warn("\n", .{});

}</lang>

Output:
Test [1/1] test "pick random element"...
  'N', 'Q', 'H', '?', 
  'D', '>', 'Q', 'G', 
  'U', 'H', 'W', 'U', 
  'N', '(', 'E', 'K', 
  'Y', 'V', 'W', 'S', 
  'U', 'V', 'H', '<', 
  'Z', 'O', 'C', 'D', 
  'Y', 'J', '?', 'L', 
All 1 tests passed.

zkl

Translation of: XPL0

<lang zkl>list:=T("hydrogen", "helium", "lithium", "beryllium", "boron"); list[(0).random(list.len())]</lang>