Advertisement
frozenking

Python classwork

Jun 25th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. 1Write your own code to generate random variates from the following distributions:
  2. a.  Exponential distribution (mean inter-arrival time of 0.25)
  3. b.  Bernoulli distribution ( p = 0.25)
  4. c.  Binomial distribution (n = 20, p = 0.5)
  5. d.  Poisson distribution (arrival rate = 4.0)
  6. Draw histograms for sequences of N random numbers for 1000, 10000 and 100000.
  7.  
  8. Poisson distribution
  9.  
  10. Here's how Wikipedia says Knuth says to do it:
  11.  
  12. init:
  13.     Let L ← e^(−λ), k ← 0 and p ← 1.
  14. do:
  15.     k ← k + 1.
  16.     Generate uniform random number u in [0,1] and let p ← p × u.
  17. while p > L.
  18. return k − 1.
  19.  
  20. In Java, that would be:
  21.  
  22. public static int getPoisson(double lambda) {
  23.  double L = Math.exp(-lambda);
  24.  double p = 1.0;
  25.  int k = 0;
  26.  
  27.  do {
  28.    k++;
  29.    p *= Math.random();
  30.  } while (p > L);
  31.  
  32.  return k - 1;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement