-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRandomUtilDemo.java
140 lines (127 loc) · 3.46 KB
/
RandomUtilDemo.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.leores.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.leores.plot.JGnuplot;
import org.leores.plot.JGnuplot.Plot;
import org.leores.util.RandomUtil;
import org.leores.util.TestUtil;
import org.leores.util.U;
import org.leores.util.data.DataTableSet;
import org.leores.util.data.Distribution;
public class RandomUtilDemo extends Demo {
public void genN() {
RandomUtil ru = new RandomUtil(null);
int[] rtn = ru.genN(5, 0, 9);
for (int i = 0; i < rtn.length; i++) {
log(rtn[i]);
}
log("---------");
rtn = ru.genN(5, 1, 7);
for (int i = 0; i < rtn.length; i++) {
log(rtn[i]);
}
}
public void getNElementSpeed() {
final RandomUtil ru = new RandomUtil(null);
final int max = 100000;
final List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < max; i++) {
list.add(i);
}
final int n = max * 9 / 10;
Runnable r0 = new Runnable() {
public void run() {
ru.getNElementsKnuthFisherYatesShuffle(list, n);
}
};
Runnable r1 = new Runnable() {
public void run() {
ru.getNElementsReservoirSampling(list, n);
}
};
Runnable r2 = new Runnable() {
public void run() {
List<Integer> rtn = new ArrayList<Integer>();
int[] ids = ru.genN(n, 0, max);
for (int i = 0; i < ids.length; i++) {
rtn.add(list.get(ids[i]));
}
}
};
Runnable rcombined = new Runnable() {
public void run() {
ru.getNElements(list, n);
}
};
TestUtil.compareRunTime(100, 3, r0, r1, r2, rcombined);
}
public void getNElementRandomQuality() {
RandomUtil ru = new RandomUtil(null);
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
Distribution dist = new Distribution("NElementRandomQuality");
for (int i = 0; i < 100000; i++) {
List<Integer> lSel = ru.getNElements(list, 2);
Collections.sort(lSel);
int val = 0;
for (int j = 0, mj = lSel.size(); j < mj; j++) {
int valj = lSel.get(j);
val += valj * Math.pow(10, mj - 1 - j);
}
dist.statDiscrete((double) val);
}
dist.sPatNumOut = ".####";
dist.calDist();
dist.print();
}
public void getNSpeed() {
final RandomUtil ru = new RandomUtil(null);
final int min = 0, max = 100000, n = max / 100;
Runnable r1 = new Runnable() {
public void run() {
ru.genNBitSet(n, min, max);
}
};
Runnable r2 = new Runnable() {
public void run() {
ru.genNReservoirSampling(n, min, max);
}
};
Runnable r3 = new Runnable() {
public void run() {
ru.genN(n, min, max);
}
};
TestUtil.compareRunTime(100, 3, r1, r2, r3);
}
public void getNRandomQuality() {
RandomUtil ru = new RandomUtil(null);
Distribution dist = new Distribution("NElementRandomQuality");
for (int i = 0; i < 100000; i++) {
int[] iSel = ru.genN(2, 1, 10);
List<Integer> lSel = U.parseList(iSel);
Collections.sort(lSel);
int val = 0;
for (int j = 0, mj = lSel.size(); j < mj; j++) {
int valj = lSel.get(j);
val += valj * Math.pow(10, mj - 1 - j);
}
dist.statDiscrete((double) val);
}
dist.sPatNumOut = ".####";
dist.calDist();
dist.print();
}
public static void demo() {
RandomUtilDemo demo = new RandomUtilDemo();
demo.genN();
demo.getNElementSpeed();
demo.getNElementRandomQuality();
demo.getNSpeed();
demo.getNRandomQuality();
}
}