2 summary:: random values with arbitrary probability distribution
3 related:: Classes/Ppoisson
4 categories:: Streams-Patterns-Events>Patterns>Random
8 Creates an integral table on instantiation (cpu intensive) which is then used by the streams to generate random values efficiently.
14 argument::distribution
15 desired probability distribution (histogram).
18 lower bound of the resulting values.
21 upper bound of the resulting values.
24 number of values to repeat.
27 resample table to this size. If the size of the distribution is smaller than 64, it is (linearly) resampled to this minimum size.
29 argument::distribution
30 set the distribution, the table is recalculated.
33 set the resample size, the table is recalculated.
40 var a = Pprob([0,0,0,0,1,1,1,1,3,3,6,6,9].scramble);
42 b.nextN(800).sort.plot("sorted distribution");
43 b.nextN(800).sort.plot("sorted distribution, again");
47 // comparison: emulate a linrand
51 x = Pfunc({ 1.0.linrand });
56 postf("Pprob mean: % linrand mean: % \n", b.nextN(800).mean, y.nextN(800).mean);
58 b.nextN(800).sort.plot("this is Pprob");
59 y.nextN(800).sort.plot("this is linrand");
65 bench { Pprob([0, 1]) } // this is fairly expensive
66 bench { 16.do { Pseq([0, 1] ! 32) } }
68 x = Pprob([0, 1]).asStream;
69 y = Pseq([0, 1], inf).asStream;
71 bench { 100.do { x.next } }; // this very efficient
72 bench { 100.do { y.next } };
78 SynthDef(\help_sinegrain,
79 { arg out=0, freq=440, sustain=0.05;
81 env = EnvGen.kr(Env.perc(0.01, sustain, 0.2), doneAction:2);
82 Out.ar(out, SinOsc.ar(freq, 0, env))
89 a = Pprob([0, 0, 1, 0, 1, 1, 0, 0], 60, 80);
93 Synth(\help_sinegrain, [\freq, t.next.midicps]);
99 a.distribution = [0, 1];
100 a.distribution = [1, 0];
101 a.distribution = [0, 0, 0, 0, 1, 0];
102 a.distribution = [0, 1, 0, 0, 0, 0];
104 // higher resolution results in a more accurate distribution: