-
Notifications
You must be signed in to change notification settings - Fork 1
/
betadist.js
executable file
·76 lines (60 loc) · 1.81 KB
/
betadist.js
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
//Beta distribution generator - adapted from "Computer Music" by Dodge and Jerse
//--by Mike Dean
//--GLOBAL-------
this.autowatch = 1; //automatically recompile everytime script is saved
//declare number of inlets and outlets
inlets = 1;
outlets = 1;
//--------------------------------------------
//------------MAIN----------------------------
//--------------------------------------------
//function runs whenever a bang is received
function bang() {
var x = 0;
var y = 0;
var ainv = 1/second_arg();
var binv = 1/third_arg();
//generate random numbers that are not 0 to avoid potential of underflow during exponentiation
while(x==0) {
x = Math.random();
}
while(y==0) {
y = Math.random();
}
//exponentiate
x = Math.pow(x,ainv);
y = Math.pow(y,binv);
//add them together
var sum = x + y;
//divide the first number by the sum
var result = x/sum;
outlet(0, result*first_arg());
}
function first_arg() {
var range = 0;
//define range
if (jsarguments[1] == undefined) {
range = 1; //set range automatically if no argument is given
} else {
range = jsarguments[1]; //otherwise, set resolution to first argument
}
return range;
}
function second_arg() {
//define a
if (jsarguments[2] == undefined) {
a = 0.5; //set a automatically if no argument is given
} else {
a = jsarguments[2]; //otherwise, set resolution to first argument
}
return a;
}
function third_arg() {
//define b
if (jsarguments[3] == undefined) {
b = 0.5; //set a automatically if no argument is given
} else {
b = jsarguments[3]; //otherwise, set resolution to first argument
}
return b;
}