-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomCs.ts
81 lines (69 loc) · 1.68 KB
/
randomCs.ts
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
import * as Random from './random';
import { postError } from './jsCommon';
inlets = 2;
outlets = 2;
var maxLength: number = 64;
var order: number[] = [0, maxLength - 1]
search:
for (let denominator = 2; denominator < 100; denominator++) {
for (let numerator = 1; numerator < denominator; numerator++) {
const fraction = numerator / denominator;
const number = Math.round(fraction * maxLength);
if (order.indexOf(number) === -1) {
order.push(number);
if (order.length === maxLength) {
break search;
}
}
}
}
let seed: number = 0;
let take: number = 16;
let allAs: number[] = [];
let allBs: number[] = [];
function msg_int(b: number) {
try {
switch (inlet) {
case 0:
take = b;
break;
case 1:
seed = b;
regenerate();
break;
}
bang();
} catch (e) {
postError(e);
}
}
function regenerate() {
allAs = [];
allBs = [];
let rng = new Random.MT(seed);
for (let i = 0; i < maxLength; i++) {
do {
var b = 2 * (rng.normal() - 0.5);
var c = 2 * (rng.normal() - 0.5);
} while (1 < Math.sqrt(b * b + c * c));
allAs.push(b);
allBs.push(c);
}
}
function bang() {
var indices = order.slice(0, take).sort(function (a, b) { return a - b });
var as = [];
var bs = [];
for (let i = 0; i < take; i++) {
as.push(allAs[indices[i]]);
bs.push(allBs[indices[i]]);
}
outlet(1, as);
outlet(0, bs);
}
function loadbang() {
regenerate();
bang();
}
let module = {};
export = {};