-
Notifications
You must be signed in to change notification settings - Fork 39
/
BelievableDataSource.java
58 lines (45 loc) · 1.25 KB
/
BelievableDataSource.java
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
import java.util.*;
/**
* BelievableDataSource
* Create test data for layout engine.
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public class BelievableDataSource implements DataSource {
public Random rnd;
public BelievableDataSource() {
// seeded, so we can reproduce results
this(2);
}
public BelievableDataSource(int seed) {
rnd = new Random(seed);
}
public Layer[] make(int numLayers, int sizeArrayLength) {
Layer[] layers = new Layer[numLayers];
for (int i = 0; i < numLayers; i ) {
String name = "Layer #" i;
float[] size = new float[sizeArrayLength];
size = makeRandomArray(sizeArrayLength);
layers[i] = new Layer(name, size);
}
return layers;
}
protected float[] makeRandomArray(int n) {
float[] x = new float[n];
// add a handful of random bumps
for (int i=0; i<5; i ) {
addRandomBump(x);
}
return x;
}
protected void addRandomBump(float[] x) {
float height = 1 / rnd.nextFloat();
float cx = (float)(2 * rnd.nextFloat() - 0.5);
float r = rnd.nextFloat() / 10;
for (int i = 0; i < x.length; i ) {
float a = (i / (float)x.length - cx) / r;
x[i] = height * Math.exp(-a * a);
}
}
}