Pick random element: Difference between revisions

Content deleted Content added
Lua
m white space and alphabet corrections
Line 64: Line 64:
Random, randint, 1, %list0%
Random, randint, 1, %list0%
MsgBox % List%randint%</lang>
MsgBox % List%randint%</lang>




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


rand() {
rand() {
Line 80: Line 77:
}
}


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


=={{header|C}}==
=={{header|C}}==
Line 112: Line 108:


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


=={{header|D}}==
=={{header|D}}==
Line 197: Line 191:
=={{header|LabVIEW}}==
=={{header|LabVIEW}}==
{{VI snippet}}<br/>[[File:LabVIEW_Pick_random_element.png]]
{{VI snippet}}<br/>[[File:LabVIEW_Pick_random_element.png]]

=={{header|Liberty BASIC}}==
=={{header|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.
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"
<lang lb>
list$ ="John Paul George Ringo Peter Paul Mary Obama Putin"
wantedTerm =int( 10 *rnd( 1))
wantedTerm =int( 10 *rnd( 1))
print "Selecting term "; wantedTerm; " in the list, which was "; word$( list$, wantedTerm, " ")
print "Selecting term "; wantedTerm; " in the list, which was "; word$( list$, wantedTerm, " ")</lang>
</lang>
Selecting term 5 in the list, which was Peter
Selecting term 5 in the list, which was Peter


=={{header|Lua}}==
=={{header|Lua}}==


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


=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 226: Line 217:
<lang Matlab> list = 1:1000;
<lang Matlab> list = 1:1000;
list(ceil(rand(1)*length(list)))</lang>
list(ceil(rand(1)*length(list)))</lang>

=={{header|NetLogo}}==
=={{header|NetLogo}}==
<lang NetLogo>;; from list containnig only literals and literal constants
<lang NetLogo>;; from list containnig only literals and literal constants
user-message one-of [ 1 3 "rooster" blue ]
user-message one-of [ 1 3 "rooster" blue ]
;; from list containing variables and reporters
;; from list containing variables and reporters
user-message one-of (list (red + 2) turtles (patch 0 0) )
user-message one-of (list (red + 2) turtles (patch 0 0) )</lang>
</lang>


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


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 262: Line 251:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>pick(v)=v[random(#v)+1]</lang>
<lang parigp>pick(v)=v[random(#v)+1]</lang>

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


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 294: Line 279:
( 2..9, <J Q K A> X~ <♠ ♣ ♥ ♦> ).pick(5); # Draw 5
( 2..9, <J Q K A> X~ <♠ ♣ ♥ ♦> ).pick(5); # Draw 5
( 2..9, <J Q K A> X~ <♠ ♣ ♥ ♦> ).pick(*); # Get a shuffled deck</lang>
( 2..9, <J Q K A> X~ <♠ ♣ ♥ ♦> ).pick(*); # Get a shuffled deck</lang>

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


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 299: Line 288:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I> declare t(0:9) character (1) static initial
<lang PL/I>
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()) );
put ( t(10*random()) );</lang>
</lang>
output:
output:
<pre>
<pre>
Line 410: Line 397:
<lang ruby>irb(main):001:0> %w(north east south west).choice
<lang ruby>irb(main):001:0> %w(north east south west).choice
=> "south"</lang>
=> "south"</lang>



=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
Line 443: Line 429:


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