Pick random element: Difference between revisions

Content deleted Content added
added Julia example
Added COBOL example. Corrected PL/I and R lang tags and Common Lisp header.
Line 180: Line 180:


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

=={{header|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>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
Line 185: Line 204:
console.log array[Math.floor(Math.random() * array.length)]</lang>
console.log array[Math.floor(Math.random() * array.length)]</lang>


=={{header|CommonLisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defvar *list* '(one two three four five))
<lang lisp>(defvar *list* '(one two three four five))


Line 455: Line 474:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I> declare t(0:9) character (1) static initial
<lang pli> declare t(0:9) character (1) static initial
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
put ( t(10*random()) );</lang>
put ( t(10*random()) );</lang>
Line 515: Line 534:


=={{header|R}}==
=={{header|R}}==
<lang r># a vector (letters are builtin)
<lang rsplus># a vector (letters are builtin)
letters
letters
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"