如何选择一个随机区域在哪个活动中?

发布于 2025-01-31 04:41:35 字数 285 浏览 2 评论 0原文

我正在创建一个模型,必须在其中模拟耳机。特别是它的位置将是我在GIS地图上创建的多个区域中的随机位置。我需要运行模型,经过片刻之后,模型必须在我创建的一个区域中在意大利南部生成一个随机的震中位置。此外,这些地区有不同的地震概率(例如,坎帕尼亚20%,卡拉布里亚(Calabria)15%等)。如何改善它?我不知道Java语言,您能指导我吗?非常感谢,问候。我已经上传了我的任何型号的图片。谢谢。

“在此处输入图像说明”

I'm creating a model in which I have to simulate an eartquake. In particular its position would be random within multiple region which I have created on my GIS map. I need to run model and after a few moments, model have to generate a random position of epicenter in South Italy, in one of regions which I created. Moreover, these regions have different probabilities to run an earthquake (for example Campania 20%, Calabria 15%, etc.). How can improve it? I don't know java language as well as, could you guide me on it? Thanks a lot, regards. I've uploaded a pic of my Anylogic model. Thanks.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2025-02-07 04:41:35

MATH.RANDOM()将返回一个伪数编号(大约)在0(包含)和1(独家)之间的分布(大约)。因此,您可以使用Math.random()<概率获取true返回,如果应该发生具有给定概率的事件,并且false(如果不是)。

在这里,一个示例还计算出了地震的实际次数,以表明概率实际上是您所期望的。

public class Application {

    public static void main(String[] args) {
        var trueCounter = 0;
        var falseCounter = 0;
        var tries = 10000;
        for (int i = 0; i < tries; i++){
            var spawnEarthquake = shouldSpawnEarthquake(0.2f);
            if(spawnEarthquake) trueCounter++;
            else falseCounter++;
        }
        System.out.printf("""
                %s tries:
                %s times an earthquake was spawned (Ratio: %s)
                %s times no earthquake was spawned (Ratio: %s)
                """, tries, trueCounter, (double)trueCounter / (double) tries, falseCounter, (double)falseCounter / (double) tries);
    }
    public static boolean shouldSpawnEarthquake(float probability){
        return Math.random() < probability;
    }

}

预期结果:

10000 tries:
1987 times an earthquake was spawned (Ratio: 0.1987)
8013 times no earthquake was spawned (Ratio: 0.8013)

您的结果可能显然有所不同,但应该在同一球场。

您需要在所有地区进行迭代,并将功能应用于每个区域,因为地震不是相互排他性的。如果地震将是相互独家的,那么您的模型/模拟不是很准确。没有什么能阻止在“同一时间”(至少据我所知)发生两次地震,因此您需要对所有地区进行这种概率。当然,当地震发生在附近,如果这种情况实际上是这种情况,您当然也可以建模地震的可能性下降。但这取决于您建模,我不是地震专家。

在这里,一个简单的实现一次,一次检查一次地区的地震。多次执行它,看到有时会产生地震,有时不会产生地震:

import java.util.*;

public class Application {
    
    // A record for the region
    // Each region as a name and an earthquake probability
    record Region(String name, float earthquakeProbability){};

    public static void main(String[] args) {
        var myRegions = List.of(
                new Region("Campania", 0.2f),
                new Region("Calabria", 0.15f)
        );

        myRegions.forEach(region -> {
            var spawnEarthquake = shouldSpawnEarthquake(region.earthquakeProbability);
            if(spawnEarthquake) System.out.printf("An earthquake occurred in %s\n", region.name());
        });
    }
    public static boolean shouldSpawnEarthquake(float probability){
        return Math.random() < probability;
    }
}

Math.random() will return a pseudorandom number with (approximately) even distribution between 0 (inclusive) and 1 (exclusive). Therefore you can use Math.random() < probability to get true return if an event with the given probability should happen and false if not.

Here an example which also calculates the actual amount of times an earthquake was spawned to show that the probability is actually what you expect.

public class Application {

    public static void main(String[] args) {
        var trueCounter = 0;
        var falseCounter = 0;
        var tries = 10000;
        for (int i = 0; i < tries; i++){
            var spawnEarthquake = shouldSpawnEarthquake(0.2f);
            if(spawnEarthquake) trueCounter++;
            else falseCounter++;
        }
        System.out.printf("""
                %s tries:
                %s times an earthquake was spawned (Ratio: %s)
                %s times no earthquake was spawned (Ratio: %s)
                """, tries, trueCounter, (double)trueCounter / (double) tries, falseCounter, (double)falseCounter / (double) tries);
    }
    public static boolean shouldSpawnEarthquake(float probability){
        return Math.random() < probability;
    }

}

Expected result:

10000 tries:
1987 times an earthquake was spawned (Ratio: 0.1987)
8013 times no earthquake was spawned (Ratio: 0.8013)

Your results might obviously differ but should be in the same ballpark.

You would need to iterate over all your regions and apply the function for each region, because earthquakes are not mutual exclusive. If earthquakes would be mutual exclusive your model/ simulation is not very accurate. There is nothing that prevents two earthquakes from happening at the "same time" (at least to my knowledge), so you need to run this probability against all of the regions. You could of course also model something like an decreased probability for an earthquake when an earthquake happens nearby if something like this is actually the case. But that's up to you modelling it, I am no earthquake expert.

Here a simple implementation that checks for an earthquake in a region once. Execute it multiple times to see that sometimes an earthquake will be spawned and sometimes, no earthquake will be spawned:

import java.util.*;

public class Application {
    
    // A record for the region
    // Each region as a name and an earthquake probability
    record Region(String name, float earthquakeProbability){};

    public static void main(String[] args) {
        var myRegions = List.of(
                new Region("Campania", 0.2f),
                new Region("Calabria", 0.15f)
        );

        myRegions.forEach(region -> {
            var spawnEarthquake = shouldSpawnEarthquake(region.earthquakeProbability);
            if(spawnEarthquake) System.out.printf("An earthquake occurred in %s\n", region.name());
        });
    }
    public static boolean shouldSpawnEarthquake(float probability){
        return Math.random() < probability;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文