Java中的三角分布 [英] Triangular distribution in Java

查看:327
本文介绍了Java中的三角分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个零件,每个零件10000次,应该适合大小写,并且零件的尺寸由均匀分布,正态分布和三角形分布给出,方法是在每个分布的附加维中随机生成数字.

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.

对于每4个部分,均需确定它们是否适合.但这不应该是一个问题.

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

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

推荐答案

放入代码此Wikipedia公式,您可以使用以下内容生成三角分布:

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));
    }
}

请注意,在您的正态分布中,您不应每次都创建一个Random对象:只能创建一次并重复使用.

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

这篇关于Java中的三角分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆