java如何计算正态分布
时间: 2023-10-27 18:13:35 浏览: 52
Java可以利用math库中的Random和.sqrt()函数,以及公式z = (x - μ) / σ来计算正态分布。具体实现可以参考以下代码:
```
import java.util.Random;
public class NormalDistribution {
public static void main(String[] args) {
double mean = 0;
double stdDev = 1;
Random rand = new Random();
// Generate a random number from a Gaussian (normal) distribution with mean 0 and standard deviation 1
double x = rand.nextGaussian();
// Calculate the z-score
double z = (x - mean) / stdDev;
// Calculate the probability density function (PDF) of the normal distribution at x
double pdf = (1 / (stdDev * Math.sqrt(2 * Math.PI))) * Math.exp(-(Math.pow(x - mean, 2) / (2 * Math.pow(stdDev, 2))));
System.out.println("Random number from normal distribution: " + x);
System.out.println("Z-score: " + z);
System.out.println("PDF at x: " + pdf);
}
}
```
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)