gpt4 book ai didi

java - Java中有没有一种方法可以生成遵循固定均值和标准差的随机数?

转载 作者:行者123 更新时间:2023-12-01 22:52:13 25 4
gpt4 key购买 nike

问题

我知道如何生成具有固定平均值和标准差的随机数。

我还希望这些值被限制在一个范围内(我理解这意味着结果不会是真正的高斯分布,而是剪裁的高斯分布)

上下文

我试图回答的更广泛的问题是

Assume you have a black box that gives out a monkey every 10 sec, theheight of the monkey is between 24 and 36 inches. The height of themonkeys generated over half n hour follows a normal distribution withmean 30.5 inches and standard deviation 2.5. While there is anotherbox in the room that causes a monkey to vanish if it’s height is below28 inches, this happens every 20 secs. Write a program to calculatethe number of monkeys left in the room after n-days (n is a userinput). For the sake of logic assume room is large enough toaccommodate an infinite number of monkeys and they have food.

最佳答案

nextGaussian() 方法返回平均值为 0、标准差为 1 的随机数。

这意味着 nextGaussian() 返回的数字将倾向于在 0 附近“聚集”,并且(大约)70% 的值将在 -1 到 1 之间。根据 nextGaussian() 返回的值,您可以缩放并移动它们以获得其他正态分布:

  • 要更改分布的 maen(平均值),请添加所需的值;

  • 要更改标准差,请乘以该值。

示例:生成平均值为 500、标准差为 100 的值:

double val = r.nextGaussian() * 100 + 500;

生成平均值为 30.5、标准差为 2.5 的值:

double val = r.nextGaussian() * 2.5 + 30.5;

70% 的值将在 28 到 33 之间。由于 99.7% 的值位于 3-sigma 范围内,因此猴子的高度在 24 到 36 之间。

关于java - Java中有没有一种方法可以生成遵循固定均值和标准差的随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com