-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscreteDistribution.java
More file actions
44 lines (38 loc) · 1.42 KB
/
Copy pathDiscreteDistribution.java
File metadata and controls
44 lines (38 loc) · 1.42 KB
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
import java.lang.Math;
public class DiscreteDistribution {
public static void main (String[] args) {
int count = args.length;
int a[] = new int[count];
int b[] = new int[count];
//System.out.println(count);
int m = Integer.parseInt(args[0]);
// define variable x to hold cumulative sum
int x = 0;
int ri;
for (int i = 0; i < count - 1; i++) {
a[i] = Integer.parseInt(args[i+1]);
//System.out.println(a[i]);
}
//System.out.println("-------");
b[0] = 0;
for (int j = 1; j < count ; j++) {
b[j] = x + a[j-1];
x = b[j];
//System.out.println(b[j]);
}
//System.out.println("-------");
for (int k = 0; k < m; k++) {
double r = Math.random() * (b[count-1]);
ri = (int) r;
//System.out.println("random # = " + r);
//System.out.println("random int # = " + ri);
for (int q = 1; q < count; q++) {
//System.out.println("b[" + (q-1) + "] = " + b[q-1] + "b[" + q + "] = " + b[q]);
if ((ri >= b[q-1]) && (ri < b[q])) {
//System.out.println("b[" + (q-1) + "] = " + b[q-1] + "b[" + q + "] = " + b[q]);
System.out.print(q + " ");
}
}
}
}
}