File: Pprob.schelp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (107 lines) | stat: -rw-r--r-- 2,248 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class:: Pprob
summary:: random values with arbitrary probability distribution
related:: Classes/Ppoisson
categories:: Streams-Patterns-Events>Patterns>Random

description::

Creates an integral table on instantiation (cpu intensive) which is then used by the streams to generate random values efficiently.

ClassMethods::

method::new

argument::distribution
desired probability distribution (histogram).

argument::lo
lower bound of the resulting values.

argument::hi
upper bound of the resulting values.

argument::length
number of values to repeat.

argument::tableSize
resample table to this size. If the size of the distribution is smaller than 64, it is (linearly) resampled to this minimum size.

argument::distribution
set the distribution, the table is recalculated.

argument::tableSize
set the resample size, the table is recalculated.

Examples::

code::
// a consistency test
(
var a = Pprob([0,0,0,0,1,1,1,1,3,3,6,6,9].scramble);
var b = a.asStream;
b.nextN(800).sort.plot("sorted distribution");
b.nextN(800).sort.plot("sorted distribution, again");
)


// comparison: emulate a linrand
(
var a, b, x, y;
a = Pprob([1, 0]);
x = Pfunc({ 1.0.linrand });

b = a.asStream;
y = x.asStream;

postf("Pprob mean: % linrand mean: % \n", b.nextN(800).mean, y.nextN(800).mean);

b.nextN(800).sort.plot("this is Pprob");
y.nextN(800).sort.plot("this is linrand");
)


// compare efficiency

bench { Pprob([0, 1]) } // this is fairly expensive
bench { 16.do { Pseq([0, 1] ! 32) } }

x = Pprob([0, 1]).asStream;
y = Pseq([0, 1], inf).asStream;

bench { 100.do { x.next } }; // this very efficient
bench { 100.do { y.next } };



// sound example
(
SynthDef(\help_sinegrain,
	{ arg out=0, freq=440, sustain=0.05;
		var env;
		env = EnvGen.kr(Env.perc(0.01, sustain, 0.2), doneAction: Done.freeSelf);
		Out.ar(out, SinOsc.ar(freq, 0, env))
	}).add;
)


(
var t;
a = Pprob([0, 0, 1, 0, 1, 1, 0, 0], 60, 80);
t = a.asStream;
Routine({
	loop({
	Synth(\help_sinegrain, [\freq, t.next.midicps]);
	0.01.wait;
	})
}).play;
)

a.distribution = [0, 1];
a.distribution = [1, 0];
a.distribution = [0, 0, 0, 0, 1, 0];
a.distribution = [0, 1, 0, 0, 0, 0];

// higher resolution results in a more accurate distribution:
a.tableSize = 512;
a.tableSize = 2048;
::