History

array.shuffle(); new example

Ge Wang committed 3 years ago

Commit cc1cd561 parent 7686e907 files · +179 −42Browse files

Showing the changes to examples/array (1 of 7 files). Show all

Unified · Split

examples/array/array_shuffle.ck+35−0
@@ -0,0 +1,35 @@
1+// name: array_shuffle.ck
2+// desc: shuffling the contents of an array; useful for tasks like random without replacement
3+// author: kunwoo, nshaheed, andrew, ge | 1.4.2.1
4+
5+// an array
6+[1,2,3,4,5] @=> int a[];
7+
8+// uncomment to print array API
9+// a.help();
10+// uncomment to explicitly set a random seed
11+// Math.srandom(11);
12+
13+// shuffle a few times
14+repeat( 10 )
15+{
16+ // print
17+ for( int i; i < a.size(); i++ )
18+ { cherr <= a[i] <= " "; } cherr <= IO.nl();
19+ // shuffle!
20+ a.shuffle();
21+}
22+
23+// a way to do random without replacement
24+repeat( 3 )
25+{
26+ cherr <= "--- random with replacment --- " <= IO.nl();
27+ // shuffle!
28+ a.shuffle();
29+ // just go through array in order after shuffle
30+ for( int i; i < a.size(); i++ )
31+ {
32+ // print the next "random" value
33+ cherr <= "next value: " <= a[i] <= IO.nl();
34+ }
35+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t