File: array_shuffle.ck

package info (click to toggle)
chuck 1.5.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 40,904 kB
  • sloc: cpp: 120,943; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 456; python: 174; perl: 86
file content (35 lines) | stat: -rw-r--r-- 840 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// name: array_shuffle.ck
// desc: shuffling the contents of an array; useful for tasks like random without replacement
// author: kunwoo, nshaheed, andrew, ge | 1.5.0.0

// an array
[1,2,3,4,5] @=> int a[];

// uncomment to print array API
// a.help();
// uncomment to explicitly set a random seed
// Math.srandom(11);

// shuffle a few times
repeat( 10 )
{
    // print
    for( int i; i < a.size(); i++ )
    { cherr <= a[i] <= " "; } cherr <= IO.nl();
    // shuffle!
    a.shuffle();
}

// one way to do random without replacement
repeat( 3 )
{
    cherr <= "--- random without replacment --- " <= IO.nl();
    // shuffle!
    a.shuffle();
    // just go through array in order after shuffle
    for( int i; i < a.size(); i++ )
    {
        // print the next "random" value
        cherr <= "next value: " <= a[i] <= IO.nl();
    }
}