random generator seed




Every Thread in sclang has a (pseudo-) random number generator that is responsible for all randomization within this thread. Each random number generator has its own seed (starting point) from which the series of values is generated. This seed can be set and after that, the randgen (being strictly deterministic) produces exactly the same numbers again. 


In order to save diskspace, you can reproduce any sequence of randomized data just by one Integer number that you can write down in your notebook..



see also: [RandSeed] [Pseed] [Thread]





// every thread, also a Routine, has a random generator seed:

(

r = Routine({

loop({#[1,2,3,4,5].choose.yield })

});

r.randSeed = 1923;

)


// using the routine to fill an array

Array.fill(7, r);


// setting the random generator seed back to our initial seed

r.randSeed = 1923;


// causes this array to be identical

Array.fill(7, r);






Inheriting Seeds


Also it is possible to set the seed of the running thread that 

all threads started within will inherit.



thisThread.randSeed = 1923;


// create a function that returns a routine


r = { Routine({

loop({#[1,2,3,4,5].choose.yield })

}) };


Array.fill(7, r.value);


// reset the seed

thisThread.randSeed = 1923;


Array.fill(7, r.value);


// use the seed to completely reproduce a sound:

(

SynthDef(\help_randomSeed, { arg out=0, freq=440;

Out.ar(out,

Line.kr(1, 0, 0.3, doneAction:2) * 

Resonz.ar(

Dust2.ar([10, 10], 270) + WhiteNoise.ar(4),

freq, 0.01) 

)

}).send(s);


SynthDef(\help_setRandomSeed, { arg seed=1956, tbus=0.0;

RandSeed.kr(tbus, seed);

}).send(s);

)


// run a patch

(

x = Synth(\help_setRandomSeed);

r = Routine({

loop({

Synth(\help_randomSeed, [\freq, rrand(440, 700)]);

0.25.wait;

})

}).play;

)


// make a reset task


(

d = 1250;// seed

t = Task({

loop({

x.set(\seed, d, \tbus, 1.0); r.randSeed = d;

0.1.wait;

x.set(\tbus, 0.0);

1.9.wait;

})

});

)


// sound starts to loop

t.start;


d = 1251; // different loop

d = 1925;



// sound is just like random again, not interested in anything..

t.stop;