-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscreteDistribution.java
More file actions
31 lines (30 loc) · 1.06 KB
/
Copy pathDiscreteDistribution.java
File metadata and controls
31 lines (30 loc) · 1.06 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class DiscreteDistribution {
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
int[] a = new int[args.length - 1];
for (int i = 0; i < args.length - 1; i++) {
a[i] = Integer.parseInt(args[i + 1]);
}
int[] S = new int[args.length];
S[0] = 0;
S[1] = a[0];
for (int i = 2; i < args.length; i++) {
S[i] = S[i - 1] + a[i - 1];
}
for (int i = 0; i < m; i++) {
int r = (int) (Math.random() * (S[args.length - 1]));
for (int j = 1; j < args.length; j++) {
if (S[j - 1] <= r && r < S[j]) {
System.out.print(j + " ");
break;
}
}
}
System.out.println();
}
}