问题
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
回答1:
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.
回答2:
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();
}
来源:https://stackoverflow.com/questions/33220176/triangular-distribution-in-java