QcPenPrinter: no need to allocate QPrintDialog on heap
[supercollider.git] / HelpSource / Classes / RandSeed.schelp
blob69b5a42cbc8d763078d79c0c1b6b4c5eff24a673
1 class:: RandSeed
2 summary:: Sets the synth's random generator seed.
3 related:: Classes/RandID
4 categories::  UGens>Generators>Stochastic, UGens>Random
7 Description::
9 When the trigger signal changes from nonpositive to positve, the synth's
10 random generator seed is reset to the given value. All synths that use
11 the same random number generator reproduce the same sequence of numbers
12 again.
15 See  link::Classes/RandID::  UGen for setting the randgen id and
16 link::Reference/randomSeed:: for the client side equivalent.
19 classmethods::
21 method::kr, ir
23 argument::trig
25 The trigger. Trigger can be any signal. A trigger happens when
26 the signal changes from non-positive to positive.
29 argument::seed
30 The random seed.
32 Examples::
34 code::
36 // start a noise patch
40 var noise, filterfreq;
41 noise = WhiteNoise.ar(0.05 ! 2) + Dust2.ar(70 ! 2);
42 filterfreq = LFNoise1.kr(3, 5500, 6000);
43 Resonz.ar(noise * 5, filterfreq, 0.5) + (noise * 0.5)
44 }.play;
47 // reset the seed at a variable rate
49 x = { arg seed=1956;
50                 RandSeed.kr(Impulse.kr(MouseX.kr(0.1, 100)), seed);
51 }.play;
56 x.set(\seed, 2001);
57 x.set(\seed, 1798);
58 x.set(\seed, 1902);
61 // above you can see that the sound of the LFNoise1 is not exactly reproduced (filter frequency)
62 // this is due to interference between the internal phase of the noise ugen and the
63 // seed setting rate.
65 // a solution is to start a new synth:
68 SynthDef("pseudorandom", { arg out, sustain=1, seed=1967, id=0;
69         var noise, filterfreq;
70         RandID.ir(id);
71         RandSeed.ir(1, seed);
74         noise = WhiteNoise.ar(0.05 ! 2) + Dust2.ar(70 ! 2);
75         filterfreq = LFNoise1.kr(3, 5500, 6000);
77         Out.ar(out,
78                 Resonz.ar(noise * 5, filterfreq, 0.5) + (noise * 0.5)
79                 *
80                 Line.kr(1, 0, sustain, doneAction:2)
81         )
83 }).send(s);
86 // the exact same sound is reproduced
88 fork {
89         loop {
90                 Synth("pseudorandom");
91                 1.1.wait; // wait a bit longer than sustain, so sounds don't overlap
92         }
96 // changing the rand seed changes the sound:
99 fork {
100         (1902..2005).do { |seed|
101                 seed.postln;
102                 3.do {
103                         Synth("pseudorandom", [\seed, seed]);
104                         1.1.wait;
105                 }
106         }
110 // cd skipper
112 fork {
113         (1902..2005).do { |seed|
114                 seed.postln;
115                 rrand(4,10).do {
116                         Synth("pseudorandom", [\seed, seed, \sustain, 0.05]);
117                         0.06.wait;
118                 }
119         }
123 // if the sounds overlap, this does not work as expected anymore
124 // sounds vary.
127 fork {
128         loop {
129                 Synth("pseudorandom");
130                 0.8.wait; // instead of 1.1
131         }
135 // rand id can be used to restrict the resetting of the seed to each voice:
138 fork {
139         var id=0;
140         (1902..2005).do { |seed|
141                 seed.postln;
142                 3.do {
143                         Synth("pseudorandom", [\seed, seed, \id, id]);
144                         id = id + 1 % 16; // there is 16 different random generators
145                         0.8.wait;
146                 }
147         }