3

I have 4 parts, every part 10000 times, which should fit into case, and the dimensions of the parts are given by uniform, normal and triangular distribution by randomly generating numbers in added dimensions of each distribution.

For each 4 parts there is decision if they fit or not. But that shouldn't be a problem.

I've managed somehow to do uniform and normal distribution:

public double uniformDistrubution(double min, double max) {
    Random rand = new Random();
    return Math.random() * max + min;
}

public double normalDistrubution(double mean, double std) {
    Random rng = new Random();
    return mean + std * rng.nextGaussian();
}

But I cannot figure out the triangular one. I've the dimensions for it:

a = 7:6, b = 8:0, c = 8:4

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Marek Valda
  • 35
  • 1
  • 5
  • There's a serious problem in your `uniformDistribution` method. The return value should be `(max - min) * Math.random() + min` -- you need to scale by the range, not the `max`. Also, you never use the `Random()` object `rand`, you should delete that line. And as per @Tunaki's suggestion, make the `Random()` object `static` and instantiate it outside the method for `normalDistribution`. – pjs Oct 20 '15 at 02:40
  • yea, found it too when i was debuging and the dimensions didn't check :) Anyway thanks – Marek Valda Oct 21 '15 at 05:31
  • Hello @pjs ! I was curious about why I should instantiate the Random() outside of the method normalDistribution. I started to read the [docu] (https://docs.oracle.com/javase/8/docs/api/java/util/Random.html) However the only find an answer. Could you please point out the reason? Thanks – DTK Oct 01 '21 at 17:23
  • 1
    @DTK Hi, sounds like a question for you to ask on a decent technical Q&A site. ;) The quick summary is there are at least two reasons. 1) It's cumulatively more [expensive](https://rules.sonarsource.com/java/RSPEC-2119) to allocate, use, and free an object with every call to a method than to allocate and free it once per program, and use it as many times as needed. In class I use the analogy of a well - a sensible person doesn't dig a new well every time they want a drink of water, they dig a well once and then return to it for as many drinks as they need. – pjs Oct 01 '21 at 21:01
  • 1
    @DTK 2) PRNGs are generators, meaning they are intended to give a *sequence* of values with behaviors that are indistinguishable from true random numbers based on statistical testing. When you create a bunch of generators in rapid succession and only use the first value from each one, there are no guarantees regarding their statistical properties. In fact, Linear Congruential Generators (which Java uses for its PRNG) are notorious for producing correlated first values if seeded sequentially based solely on time. Java avoids this with its default seeding by scrambling with some additional info. – pjs Oct 01 '21 at 21:02
  • @pjs Thank you very much for the clarifications. Indeed now that you pointed out it is cristal clear. – DTK Oct 09 '21 at 19:16
  • 1
    @DTK You're welcome. I'm a retired professor, so I like explaining things and my wife likes it when I'm explaining to people other than her. – pjs Oct 09 '21 at 20:56

2 Answers2

8

Putting to code this Wikipedia formula, you can generate a triangular distribution with the following:

public double triangularDistribution(double a, double b, double c) {
    double F = (c - a) / (b - a);
    double rand = Math.random();
    if (rand < F) {
        return a + Math.sqrt(rand * (b - a) * (c - a));
    } else {
        return b - Math.sqrt((1 - rand) * (b - a) * (b - c));
    }
}

As a side note, in your normal distribution, you should not create a Random object each time: create it only once and reuse it.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
3

To add to excellent answer by @Tunaki, if you sample in symmetric triangle, you could get by using Irwin-Hall distribution with two degrees of freedom (and then scale it of course)

Link https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution

Code

public double IH2() {
    return Math.random() + Math.random();
}
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64