tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1 @@
Demonstrate how to pick a random element from a list.

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1,6 @@
:set-state-ok t
(defun pick-random-element (xs state)
(mv-let (idx state)
(random$ (len xs) state)
(mv (nth idx xs) state)))

View file

@ -0,0 +1,30 @@
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;

View file

@ -0,0 +1,11 @@
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_query(l, drand(5)));
o_byte('\n');

View file

@ -0,0 +1,3 @@
list := ["abc", "def", "gh", "ijklmnop", "hello", "world"]
Random, randint, 1, % list.MaxIndex()
MsgBox % List[randint]

View file

@ -0,0 +1,4 @@
list := "abc,def,gh,ijklmnop,hello,world"
StringSplit list, list, `,
Random, randint, 1, %list0%
MsgBox % List%randint%

View file

@ -0,0 +1,13 @@
'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)

View file

@ -0,0 +1,4 @@
DIM list$(5)
list$() = "The", "five", "boxing", "wizards", "jump", "quickly"
chosen% = RND(6)
PRINT "Item " ; chosen% " was chosen which is '" list$(chosen%-1) "'"

View file

@ -0,0 +1,2 @@
blsq ) "ABCDEFG"123456 0 6rn-]!!
'G

View file

@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
char array[] = { 'a', 'b', 'c' };
srand( time( 0 ) );
printf( "%c\n", array[ rand() % 3 ] );
return 0;
}

View file

@ -0,0 +1 @@
(rand-nth coll)

View file

@ -0,0 +1 @@
(nth coll (rand-int (count coll)))

View file

@ -0,0 +1,2 @@
array = [1,2,3]
console.log array[Math.floor(Math.random() * array.length)]

View file

@ -0,0 +1,7 @@
import std.stdio, std.random;
void main() {
auto items = ["foo", "bar", "baz"];
auto r = items[uniform(0, $)];
writeln(r);
}

View file

@ -0,0 +1 @@
print choose [ "one" "two" "chicken" ]

View file

@ -0,0 +1,2 @@
constant s = {'a', 'b', 'c'}
puts(1,s[rand($)])

View file

@ -0,0 +1,2 @@
( scratchpad ) { "a" "b" "c" "d" "e" "f" } random .
"a"

View file

@ -0,0 +1,14 @@
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))])
}

View file

@ -0,0 +1,7 @@
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]}"
}

View file

@ -0,0 +1,6 @@
import Random (randomRIO)
pick :: [a] -> IO a
pick xs = randomRIO (0, length xs - 1) >>= return . (xs !!)
x <- pick [1 2 3]

View file

@ -0,0 +1,5 @@
import Data.Random
import Data.Random.Source.DevRandom
import Data.Random.Extras
x <- runRVar (choice [1 2 3]) DevRandom

View file

@ -0,0 +1,4 @@
procedure main()
L := [1,2,3] # a list
x := ?L # random element
end

View file

@ -0,0 +1,2 @@
({~ ?@#) 'abcdef'
b

View file

@ -0,0 +1,4 @@
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

View file

@ -0,0 +1,2 @@
var array = [1,2,3];
return array[Math.floor(Math.random() * array.length)];

View file

@ -0,0 +1,2 @@
1?"abcdefg"
,"e"

View file

@ -0,0 +1,3 @@
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, " ")

View file

@ -0,0 +1 @@
pick [1 2 3]

View file

@ -0,0 +1,3 @@
math.randomseed(os.time())
local a = {1,2,3}
print(a[math.random(1,#a)])

View file

@ -0,0 +1,2 @@
list = {'a','b','c'};
list{ceil(rand(1)*length(list))}

View file

@ -0,0 +1,2 @@
list = 1:1000;
list(ceil(rand(1)*length(list)))

View file

@ -0,0 +1,2 @@
RandomChoice[{a, b, c}]
->c

View file

@ -0,0 +1,4 @@
;; 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) )

View file

@ -0,0 +1,13 @@
/* 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

View file

@ -0,0 +1,3 @@
let list_rand lst =
let len = List.length lst in
List.nth lst (Random.int len)

View file

@ -0,0 +1,3 @@
let array_rand ary =
let len = Array.length ary in
ary.(Random.int len)

View file

@ -0,0 +1,2 @@
values := [1, 2, 3];
value := values[(Float->Random() * 100.0)->As(Int) % values->Size()];

View file

@ -0,0 +1 @@
pick(v)=v[random(#v)+1]

View file

@ -0,0 +1,2 @@
$arr = array('foo', 'bar', 'baz');
$x = $arr[array_rand($arr)];

View file

@ -0,0 +1,3 @@
declare t(0:9) character (1) static initial
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
put ( t(10*random()) );

View file

@ -0,0 +1,9 @@
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.

View file

@ -0,0 +1,3 @@
(1..6).roll; # return 1 random value in the range 1 through 6
(1..6).roll(3); # return a list of 3 random values in the range 1 through 6
(1..6).roll(*); # return a lazy infinite list of random values in the range 1 through 6

View file

@ -0,0 +1,5 @@
# define the deck
constant deck = 2..9, <J Q K A> X~ <♠ ♣ ♥ ♦>;
deck.pick; # Pick a card
deck.pick(5); # Draw 5
deck.pick(*); # Get a shuffled deck

View file

@ -0,0 +1 @@
@array[@array * rand]

View file

@ -0,0 +1 @@
say Bool.pick; # returns either True or False

View file

@ -0,0 +1,2 @@
my @array = qw(a b c);
print $array[ rand @array ];

View file

@ -0,0 +1 @@
(get Lst (rand 1 (length Lst)))

View file

@ -0,0 +1,40 @@
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
#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

View file

@ -0,0 +1,3 @@
>>> import random
>>> random.choice(['foo', 'bar', 'baz'])
'baz'

View file

@ -0,0 +1,12 @@
# a vector (letters are builtin)
letters
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
# [20] "t" "u" "v" "w" "x" "y" "z"
# picking one element
sample(letters, 1)
# [1] "n"
# picking some elements with repetition, and concatenating to get a word
paste(sample(letters, 10, rep=T), collapse="")
# [1] "episxgcgmt"

View file

@ -0,0 +1,21 @@
/*REXX program to pick a random element from a list (tongue in cheek). */
_= '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 irdium platinum'
_=_ 'gold mercury thallium lead bismuth polonium astatine radon francium'
_=_ 'radium actinium thorium protactinium uranium neptunium plutonium'
_=_ 'americium curium berkelium californium einsteinum fermium mendelevium'
_=_ 'nobelium lawrencium rutherfordium dubnium seaborgium bohrium hassium'
_=_ 'meitnerium darmstadtium roentgenium copernicium Ununtrium'
w=words(_)
say 'random element =' word(_,random(1,w))
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,9 @@
/* 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)))

View file

@ -0,0 +1,4 @@
irb(main):001:0> %w(north east south west).sample
=> "west"
irb(main):002:0> (1..100).to_a.sample(2)
=> [17, 79]

View file

@ -0,0 +1,2 @@
irb(main):001:0> %w(north east south west).choice
=> "south"

View file

@ -0,0 +1,3 @@
list$ = "a,b,c,d,e,f,g,h,i,j"
letter = rnd(1) * 10
print "Selected letter:"; word$(list$,letter,",")

View file

@ -0,0 +1,6 @@
$ include "seed7_05.s7i";
const proc: main is func
begin
writeln(rand([] ("foo", "bar", "baz")));
end func;

View file

@ -0,0 +1 @@
x := #(1 2 3) atRandom.

View file

@ -0,0 +1,6 @@
$$ 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

View file

@ -0,0 +1,3 @@
@(do (defun randelem (vec)
(vecref vec (random nil (length vec)))))
@(bind x @(randelem #("a" "b" "c" "d")))

View file

@ -0,0 +1,4 @@
proc randelem {list} {
lindex $list [expr {int(rand()*[llength $list])}]
}
set x [randelem {1 2 3 4 5}]

View file

@ -0,0 +1,5 @@
code Ran=1, Text=12;
int List;
[List:= ["hydrogen", "helium", "lithium", "beryllium", "boron"]; \(Thanks REXX)
Text(0, List(Ran(5)));
]