0
ExponentialDistribution exp = new ExponentialDistribution(4.0);
        for(int i = 1; i < 20; i++){
            timestamp[i] = (int)exp.sample() + 1+timestamp[i-1];

Here timestamp is an array of integers and a random value is assigned to it with above condition. What does (int)exp.sample() does and how it assigns a random value to i?

  • 3
    No idea. What is `ExponentialDistribution`? – Boris the Spider Aug 16 '16 at 18:40
  • Apparently it's in apache.commons package: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/ExponentialDistribution.html The main reason I'm sharing this though is to as an excuse to have a meaningful comment that I can also say: your name just has to be a reference to The Who, right? – Lord Farquaad Aug 16 '16 at 18:44
  • 4
    `(int)exp.sample()` calls the method `sample()` on the object referenced by `exp`, then casts the return value to `int`. All ***very*** basic Java stuff. Suggestion: Learn Java. – Andreas Aug 16 '16 at 18:44
  • And there is nothing in the code that assigns a random value to i. – FredK Aug 16 '16 at 18:52
  • Also, one hopes that timestamp[0] has been assigned a value somewhere before the code shown. – FredK Aug 16 '16 at 18:54
  • @FredK (int)exp.sample() assigns i the random value and beforehand its assigned value 0 – Sarabjeet Singh Aug 16 '16 at 19:01

2 Answers2

0

From the ExponentialDistribution API: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/ExponentialDistribution.html#sample()

public double sample()

Generate a random value sampled from this distribution. The default implementation uses the inversion method.

Algorithm Description: this implementation uses the Inversion Method to generate exponentially distributed random values from uniform deviates.

So when you see (int)exp.sample(), we are converting it to this random value it selected. exp is the instance of ExponentialDistribution(4.0) we've created, so exp.sample() calls ExponentialDistribution's sample() method.

Now for the (int).

Placing an Object type or primitive type in parenthesis (int this case (int)) prior to another Object/primitive is called "casting". Casting variables in Java

Pretty much, since sample() returns a double, but timestamp stores ints, we need to switch one of their types (they must match), and it's waaaaay easier to change exp.sample()'s.

So, all in all, this says "Take the object exp, call it's method sample(), and then convert that into an int".

Community
  • 1
  • 1
Lord Farquaad
  • 712
  • 1
  • 12
  • 33
  • That stuff i also went through, but i am not getting how the following output is generated. – Sarabjeet Singh Aug 16 '16 at 18:57
  • timestamp gives value of timestamp[i] and ran gives (int)exp.sample(). I want know if there is a particular set of numbers from which ran is generated or it depends upon i in the loop condition?........ timestamp = 9 ran= 8, timestamp=10 ran=7, timestamp = 13 ran=6, timestamp=26, ran=6, timestamp=34, ran=3, timestamp=35, ran=7, timestamp40ran3 timestamp41ran3 timestamp43ran7 timestamp45ran4 timestamp48ran9 timestamp51ran11 timestamp53ran10 timestamp58ran3 timestamp63ran5 timestamp65ran3 timestamp67ran8 timestamp68ran3 timestamp72ran3 – Sarabjeet Singh Aug 16 '16 at 19:00
  • I don't see a `ran` and what you just commented is 100% illegible to me. But I updated my explanation. If you're after how it grabs the value from `sample()`, it just grabs it from a exponential distribution with a mean of 4. If you want to know even more about how it does it, here's the source code: http://commons.apache.org/proper/commons-math/apidocs/src-html/org/apache/commons/math3/distribution/ExponentialDistribution.html – Lord Farquaad Aug 16 '16 at 19:06
  • @Sjs You neither provided any output nor what the variable `ran` is supposed to be. – QBrute Aug 16 '16 at 19:09
  • May be i got a bit more complexed. ran is nothing, i just used it to denote the output value of (int)exp.sample(), using println. So after"......", i have just pasted the output the loop generated by printing timestamp[i] and (int)exp.sample(). When it says timestamp = 9 ran= 8, it means when loop runs at i=0, (int)exp.sample() gave output of 8 and timestamp[i] gave 9. May be you could pardon a newbie for the illeligibilty. – Sarabjeet Singh Aug 16 '16 at 19:16
  • I no longer know what you're asking. Each index of `timestamp` is set to a randomly grabbed sample plus 1 plus the value at `timestamp` before this one. So every value is getting bigger. Looks like you're creating a discrete CDF or something like that. Well... I guess not, since your values are greater than 1, but some sort of cumulative function... – Lord Farquaad Aug 16 '16 at 19:19
  • Ohk so 4.0 means it grabs value from exp distribution of mean of 4. That helped. Thanks. So if i change to 5.0, it will take value from mean of 5 , if i am not wrong? – Sarabjeet Singh Aug 16 '16 at 19:20
0

The class ExponentialDistribution generates a pseudo random number following the Exponential Distribution. It is pseudo because it is not truly random. It is used an equation to generate every single random number you get.

The method exp.sample() returns the next random number in the sequence. Using (int) you get just the integer part of it.

Manoel Campos
  • 933
  • 9
  • 12