-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipfGenerator.java
72 lines (59 loc) · 1.83 KB
/
ZipfGenerator.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
package EE5903;
import java.util.Random;
//credit: https://diveintodata.org/2009/09/13/zipf-distribution-generator-in-java/ [Online; December 2017]
public class ZipfGenerator {
private Random rnd = new Random(System.currentTimeMillis());
private int size;
private double skew;
private double bottom = 0;
public ZipfGenerator(int size, double skew) {
this.size = size;
this.skew = skew;
for(int i=1;i <= size; i++) {
this.bottom += (1/Math.pow(i, this.skew));
}
}
// the next() method returns an random rank id.
// The frequency of returned rank ids are follows Zipf distribution.
public int next() {
int rank;
double friquency = 0;
double dice;
rank = rnd.nextInt(size)+1;
friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
while(!(dice < friquency)) {
rank = rnd.nextInt(size)+1;
friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
dice = rnd.nextDouble();
}
return rank;
}
// This method returns a probability that the given rank occurs.
public double getProbability(int rank) {
return (1.0d / Math.pow(rank, this.skew)) / this.bottom;
}
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("usage: ./zipf size skew");
System.exit(-1);
}
ZipfGenerator zipf = new ZipfGenerator(Integer.valueOf(args[0]),
Double.valueOf(args[1]));
for(int i= 1;i <= 10; i++) {
System.out.println(i+" "+zipf.getProbability(i));
}
//use size = 10 and skew = 2 for testing below
int hist [] = new int [12];
for(int i=0;i<12;i++) {
hist[i] = 0;
}
System.out.println("Testing the probability distribution:");
int sum = 0;
for(int i= 1;i <= 1000000; i++) {
hist[zipf.next()]++;
}
for(int i=0;i<12;i++)
System.out.println(i+" "+hist[i]/1000000.0);
}
}