bumping version to 3.5-rc1
[supercollider.git] / Help / Core / Kernel / randomSeed.html
blob19d2329fe7a0abe3a57d114ea404997586c9b415
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Helvetica; min-height: 16.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0021e7}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #ad140d}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #001bb9}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #2c7014}
20 span.s1 {font: 13.0px Helvetica}
21 span.s2 {color: #0000ff}
22 span.s3 {color: #000000}
23 span.s4 {text-decoration: underline}
24 span.s5 {color: #001bb9}
25 span.s6 {color: #2c7014}
26 span.s7 {color: #ad140d}
27 span.Apple-tab-span {white-space:pre}
28 </style>
29 </head>
30 <body>
31 <p class="p1"><b>random generator seed</b></p>
32 <p class="p2"><br></p>
33 <p class="p2"><br></p>
34 <p class="p2"><br></p>
35 <p class="p3">Every Thread in sclang has a (pseudo-) random number generator that is responsible for all <span class="Apple-tab-span"> </span>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.<span class="s1"><span class="Apple-converted-space"> </span></span></p>
36 <p class="p4"><br></p>
37 <p class="p3">In order to save diskspace, you can reproduce any sequence of randomized data just by one <a href="../../Math/Integer.html"><span class="s2">Integer</span></a> number that you can write down in your notebook..</p>
38 <p class="p5"><br></p>
39 <p class="p5"><br></p>
40 <p class="p6"><span class="s3">see also: [<a href="../../UGens/Random/RandSeed.html"><span class="s4">RandSeed</span></a>] [<a href="../../Streams-Patterns-Events/Patterns/Pseed.html"><span class="s4">Pseed</span></a>] [<a href="Thread.html"><span class="s4">Thread</span></a>]</span></p>
41 <p class="p2"><br></p>
42 <p class="p2"><br></p>
43 <p class="p2"><br></p>
44 <p class="p2"><br></p>
45 <p class="p7">// every thread, also a Routine, has a random generator seed:</p>
46 <p class="p8">(</p>
47 <p class="p9"><span class="s3">r = </span>Routine<span class="s3">({</span></p>
48 <p class="p8"><span class="Apple-tab-span"> </span>loop({#[1,2,3,4,5].choose.yield })</p>
49 <p class="p8">});</p>
50 <p class="p8">r.randSeed = 1923;</p>
51 <p class="p8">)</p>
52 <p class="p2"><br></p>
53 <p class="p7">// using the routine to fill an array</p>
54 <p class="p8"><span class="s5">Array</span>.fill(7, r);</p>
55 <p class="p2"><br></p>
56 <p class="p7">// setting the random generator seed back to our initial seed</p>
57 <p class="p8">r.randSeed = 1923;</p>
58 <p class="p2"><br></p>
59 <p class="p7">// causes this array to be identical</p>
60 <p class="p8"><span class="s5">Array</span>.fill(7, r);</p>
61 <p class="p2"><br></p>
62 <p class="p2"><br></p>
63 <p class="p2"><br></p>
64 <p class="p2"><br></p>
65 <p class="p2"><br></p>
66 <p class="p3"><b>Inheriting Seeds</b></p>
67 <p class="p2"><br></p>
68 <p class="p3"><span class="Apple-tab-span"> </span>Also it is possible to set the seed of the running thread that<span class="Apple-converted-space"> </span></p>
69 <p class="p3"><span class="Apple-tab-span"> </span>all threads started within will inherit.</p>
70 <p class="p2"><br></p>
71 <p class="p2"><br></p>
72 <p class="p8"><span class="s5">thisThread</span>.randSeed = 1923;</p>
73 <p class="p2"><br></p>
74 <p class="p7">// create a function that returns a routine</p>
75 <p class="p2"><br></p>
76 <p class="p8">r = { <span class="s5">Routine</span>({</p>
77 <p class="p8"><span class="Apple-tab-span"> </span>loop({#[1,2,3,4,5].choose.yield })</p>
78 <p class="p8">}) };</p>
79 <p class="p2"><br></p>
80 <p class="p8"><span class="s5">Array</span>.fill(7, r.value);</p>
81 <p class="p2"><br></p>
82 <p class="p7">// reset the seed</p>
83 <p class="p8"><span class="s5">thisThread</span>.randSeed = 1923;</p>
84 <p class="p2"><br></p>
85 <p class="p8"><span class="s5">Array</span>.fill(7, r.value);</p>
86 <p class="p2"><br></p>
87 <p class="p7">// use the seed to completely reproduce a sound:</p>
88 <p class="p8">(</p>
89 <p class="p8"><span class="s5">SynthDef</span>(<span class="s6">\help_randomSeed</span>, { <span class="s5">arg</span> out=0, freq=440;</p>
90 <p class="p8"><span class="Apple-tab-span"> </span><span class="s5">Out</span>.ar(out,</p>
91 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">Line</span>.kr(1, 0, 0.3, doneAction:2) *<span class="Apple-converted-space"> </span></p>
92 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">Resonz</span>.ar(</p>
93 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">Dust2</span>.ar([10, 10], 270) + <span class="s5">WhiteNoise</span>.ar(4),</p>
94 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>freq, 0.01)<span class="Apple-converted-space"> </span></p>
95 <p class="p8"><span class="Apple-tab-span"> </span>)</p>
96 <p class="p8">}).send(s);</p>
97 <p class="p2"><br></p>
98 <p class="p8"><span class="s5">SynthDef</span>(<span class="s6">\help_setRandomSeed</span>, { <span class="s5">arg</span> seed=1956, tbus=0.0;</p>
99 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">RandSeed</span>.kr(tbus, seed);</p>
100 <p class="p8">}).send(s);</p>
101 <p class="p8">)</p>
102 <p class="p2"><br></p>
103 <p class="p7">// run a patch</p>
104 <p class="p8">(</p>
105 <p class="p10"><span class="s3">x = </span><span class="s5">Synth</span><span class="s3">(</span>\help_setRandomSeed<span class="s3">);</span></p>
106 <p class="p9"><span class="s3">r = </span>Routine<span class="s3">({</span></p>
107 <p class="p8"><span class="Apple-tab-span"> </span>loop({</p>
108 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">Synth</span>(<span class="s6">\help_randomSeed</span>, [<span class="s6">\freq</span>, rrand(440, 700)]);</p>
109 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.25.wait;</p>
110 <p class="p8"><span class="Apple-tab-span"> </span>})</p>
111 <p class="p8">}).play;</p>
112 <p class="p8">)</p>
113 <p class="p2"><br></p>
114 <p class="p7">// make a reset task</p>
115 <p class="p2"><br></p>
116 <p class="p8">(</p>
117 <p class="p8">d = 1250;<span class="s7">// seed</span></p>
118 <p class="p8">t = <span class="s5">Task</span>({</p>
119 <p class="p8"><span class="Apple-tab-span"> </span>loop({</p>
120 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x.set(<span class="s6">\seed</span>, d, <span class="s6">\tbus</span>, 1.0); r.randSeed = d;</p>
121 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.1.wait;</p>
122 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>x.set(<span class="s6">\tbus</span>, 0.0);</p>
123 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1.9.wait;</p>
124 <p class="p8"><span class="Apple-tab-span"> </span>})</p>
125 <p class="p8">});</p>
126 <p class="p8">)</p>
127 <p class="p2"><br></p>
128 <p class="p7">// sound starts to loop</p>
129 <p class="p8">t.start;</p>
130 <p class="p2"><br></p>
131 <p class="p7"><span class="s3">d = 1251; </span>// different loop</p>
132 <p class="p8">d = 1925;</p>
133 <p class="p2"><br></p>
134 <p class="p2"><br></p>
135 <p class="p7">// sound is just like random again, not interested in anything..</p>
136 <p class="p8">t.stop;</p>
137 <p class="p2"><br></p>
138 <p class="p2"><br></p>
139 </body>
140 </html>