Debian Codegraph @

examples/math/randomize.ck

raw
1 //-------------------------------------------------------------------
2 // name: randomize()
3 // desc: random number generation example...
4 // to show the effects of alternating between
5 // 1) explicitly seeding the random number generator (RNG)
6 // -- AND --
7 // 2) implicitly seeding the RNG with a practially
8 // unpredicable seed
9 //
10 // requires: chuck-1.5.0.4 or higher
11 //
12 // author: Ge Wang (https://ccrma.stanford.edu/~ge/)
13 // date: Summer 2023
14 //-------------------------------------------------------------------
15
16 // explicit seed
17 10513 => int EXPLICIT_SEED;
18 // how many to print each round
19 8 => int HOW_MANY;
20 // how long to wait between each number
21 100::ms => dur T;
22
23 fun void generate( int howMany, dur T, int indents )
24 {
25 int i;
26 repeat(howMany)
27 {
28 T => now;
29 repeat(indents) cherr <= " ";
30 <<< ++i, Math.random2(1,100) >>>;
31 }
32 }
33
34 while( true )
35 {
36 // explicit seeding
37 <<< "--------------------------------------", "" >>>;
38 <<< "seeding RNG with", EXPLICIT_SEED, "SAME EACH TIME!" >>>;
39 <<< "--------------------------------------", "" >>>;
40 Math.srandom( EXPLICIT_SEED );
41 // generate
42 generate( HOW_MANY, T, 0 );
43
44 // wait a bit
45 5*T => now;
46
47 <<< "********************", "" >>>;
48 <<< "SHAKING THINGS UP!!!", "" >>>;
49 <<< "********************", "" >>>;
50 Math.randomize();
51 // generate
52 generate( HOW_MANY, T, 16 );
53
54 // wait a bit
55 5*T => now;
56 }