英文:
Probability in Java (AnyLogic)
问题
我想模拟生产良品的概率,以及带有80%概率的缺陷品。
[我看到可以生成数字并查看它们是否等于我寻找的概率][1]。但是我不太擅长Java编程,AnyLogic与Java有些不同,我不能调用库等。
我尝试过以下代码,但它不起作用。只有 `parts_count` 一直在递增,但 `parts_defect` 保持在0。我做错了什么?谢谢
double probability = normal();
if (-2 < probability || probability < 2) {
parts_count += 1;
} else if (-2 > probability || probability > 2) {
parts_defect += 1;
}
PS. normal() 生成具有标准差为1和均值为0的正态分布。我知道2 * sigma 不等于80%,我只是为了举例尝试。
[1]: https://stackoverflow.com/a/8183876/10511873
英文:
I would like to simulate the probability of producing good products and also defects with let's say 80% probability.
I've seen that it's possible to generate numbers and see if they are equal to the probability I'm looking for. But I'm not that good at Java programming and AnyLogic is a bit different than Java in that I can't call libraries etc I think.
I have tried this but it doesn't work. Only parts_count
keeps getting incremented but not parts_defect
which stays at 0 during the whole simulation. What am I doing wrong? Thanks
double probability = normal();
if ( -2<probability || probability<2){
parts_count += 1;
} else if ( -2>probability || probability>2){
parts_defect +=1;
}
PS. normal() generates a normal distribution with sigma = 1 and mean = 0. I know 2*sigma is not equal to 80% I was just trying out for the sake of the example.
答案1
得分: 1
因为如果 (-2 < probability || probability < 2) 这个条件实际上等同于说概率可以取任何值。
你在表达
概率大于-2或概率小于2...
这对世界上所有的数字来说都是成立的。
相反,你应该这样做:
if (abs(probability) < 2)
parts_count += 1;
else
parts_defects += 1;
英文:
That's because if ( -2<probability || probability<2){ is actually the same as saying that probability can take any value
you are saying
probability bigger than -2 or probability smaller than 2...
This is true for all the numbers of the world
Instead you should do
if( abs(probability)<2)
parts_count+=1;
else
parts_defects+=1;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论