File: randomize.ck

package info (click to toggle)
chuck 1.5.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,056 kB
  • sloc: cpp: 123,473; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 457; python: 174; perl: 86
file content (56 lines) | stat: -rw-r--r-- 1,482 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//-------------------------------------------------------------------
// name: randomize()
// desc: random number generation example...
//       to show the effects of alternating between
//       1) explicitly seeding the random number generator (RNG)
//       -- AND --
//       2) implicitly seeding the RNG with a practially
//          unpredicable seed
//
// requires: chuck-1.5.0.4 or higher
//
// author: Ge Wang (https://ccrma.stanford.edu/~ge/)
// date: Summer 2023
//-------------------------------------------------------------------

// explicit seed
10513 => int EXPLICIT_SEED;
// how many to print each round
8 => int HOW_MANY;
// how long to wait between each number
100::ms => dur T;

fun void generate( int howMany, dur T, int indents )
{
    int i;
    repeat(howMany)
    {
        T => now;
        repeat(indents) cherr <= " ";
        <<< ++i, Math.random2(1,100) >>>;
    }
}

while( true )
{
    // explicit seeding
    <<< "--------------------------------------", "" >>>;
    <<< "seeding RNG with", EXPLICIT_SEED, "SAME EACH TIME!" >>>;
    <<< "--------------------------------------", "" >>>;
    Math.srandom( EXPLICIT_SEED );
    // generate
    generate( HOW_MANY, T, 0 );

    // wait a bit
    5*T => now;    
    
    <<< "********************", "" >>>;
    <<< "SHAKING THINGS UP!!!", "" >>>;
    <<< "********************", "" >>>;
    Math.randomize();
    // generate
    generate( HOW_MANY, T, 16 );

    // wait a bit
    5*T => now;    
}