Pick random element: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 1: | Line 1: | ||
{{task|Basic language learning}} |
{{task|Basic language learning}} |
||
Demonstrate how to pick a random element from a list. |
Demonstrate how to pick a random element from a list. |
||
=={{header|Ada}}== |
|||
<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 |
|||
-- Index: Natural := Natural(Rnd.Random(Gen) * Float(A'Length)); |
|||
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> |
|||
Sample Output: <pre>horanohesuhodinahiru. desehonirosedisinelo, losihehederidonolahe?</pre> |
|||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
Line 14: | Line 49: | ||
o_byte(l_query(l, drand(5))); |
o_byte(l_query(l, drand(5))); |
||
o_byte('\n');</lang> |
o_byte('\n');</lang> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||