SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Collision.java
1package tutorial;
2import umontreal.ssj.probdist.PoissonDist;
3import umontreal.ssj.rng.*;
4import umontreal.ssj.stat.*;
5import umontreal.ssj.util.Chrono;
6
7public class Collision {
8 int k; // Number of locations.
9 int m; // Number of items.
10 double lambda; // Theoretical expectation of C (asymptotic).
11 boolean[] used; // Locations already used.
12 int maxCounts; // Values of C >= maxCounts are aggregated.
13 int[] counts; // Counts the number of occurrences of each value of C.
14 PoissonDist poisson; // Will be a Poisson distribution with mean lambda.
15
16 public Collision (int k, int m, int maxCounts) {
17 this.k = k;
18 this.m = m;
19 lambda = (double) m * m / (2.0 * k);
20 used = new boolean[k];
21 this.maxCounts = maxCounts;
22 counts = new int[maxCounts + 1];
23 poisson = new PoissonDist(lambda);
24 }
25
26 // Generates and returns the number of collisions.
27 public int simulate (RandomStream stream) {
28 int C = 0;
29 for (int i = 0; i < k; i++) used[i] = false;
30 for (int j = 0; j < m; j++) {
31 int loc = stream.nextInt (0, k-1);
32 if (used[loc]) C++;
33 else used[loc] = true;
34 }
35 return C;
36 }
37
38 // Performs n indep. runs using stream and collects statistics in statC.
39 public void simulateRuns(int n, RandomStream stream, Tally statC) {
40 statC.init();
41 int C;
42 for (int c = 0; c < maxCounts; c++)
43 counts[c] = 0;
44 for (int i = 0; i < n; i++) {
45 C = simulate(stream);
46 statC.add(C);
47 if (C > maxCounts)
48 C = maxCounts;
49 counts[C]++;
50 }
51 }
52
53 public static void main (String[] args) {
54 int k = 10000; int m = 500;
55 int maxCounts = 30;
56 int n = 100000;
57 Collision col = new Collision(k, m, maxCounts);
58 Tally statC = new Tally("Statistics on collisions");
59 Chrono timer = new Chrono();
60 col.simulateRuns(n, new MRG32k3a(), statC);
61 System.out.println("Total CPU time: " + timer.format() + "\n");
63 System.out.println(statC.report(0.95, 3));
64 System.out.println ("Theoretical mean: lambda = " + col.lambda + "\n");
65
66 System.out.println("Counters:\n"
67 + " c count Poisson expect.\n");
68 for (int c = 0; c <= col.maxCounts; c++) {
69 System.out.printf(" %2d %10d %12.1f%n", c, col.counts[c],
70 n * col.poisson.prob(c));
71 }
72 }
73}
Extends the class DiscreteDistributionInt for the Poisson distribution slaw00a  (page 325) with mean.
double prob(int x)
Returns , the probability of .
Extends the abstract class RandomStreamBase by using as a backbone (or main) generator the combined m...
Definition MRG32k3a.java:46
A subclass of StatProbe.
Definition Tally.java:47
void setConfidenceIntervalStudent()
Indicates that a confidence interval on the true mean, based on the normality assumption,...
Definition Tally.java:582
void init()
Initializes the statistical collector.
Definition Tally.java:91
void add(double x)
Gives a new observation x to the statistical collector.
Definition Tally.java:109
String report()
Returns a formatted string that contains a report on this probe.
Definition Tally.java:397
String format()
Converts the CPU time used by the program since its last call to init for this AbstractChrono to a St...
The Chrono class extends the umontreal.ssj.util.AbstractChrono class and computes the CPU time for th...
Definition Chrono.java:37
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...