java - 布冯针模拟
问题描述
import java.lang.Math;
public class BuffonNeedle
{
public static void main(String args[])
{
double drops = 100;
int hit = 0;
for(int r=1; r<=6; r++)
{
for(int i = 1; i <= drops; i++)
{
double y = Math.random() * 2;
if(Math.sin(Math.random()*Math.PI) + y >= 2.){
hit++;
}
}
System.out.println(drops/hit);
drops = drops * 10;
}
}
}
当约束相距 2 英寸且针为 1 英寸时,为什么这不会产生Pi ?
模拟运行 6 次:100、1000、10000、100000、1000000、10000000。
解决方案
你需要重置hit
System.out.println(drops/hit);
drops = drops * 10;
hit = 0; //<------reset
推荐阅读
- javascript - Jinja2 访问 javascript 中的字典列表
- php - Symfony 4 表单 - 简单的服务器端验证和 php 类型声明
- python-3.x - 列表操作和使用 STDIN
- logstash - 如何在logstash中打印或存储大型日志文件的所有跳过行
- vb.net - 欢迎。你能给我一个代码,如果它没有通过本地网络连接到数据库,它会给我一个错误消息
- javascript - 范围选择器日期在 Highcharts.Chart() 中始终为 1970-01-01
- angular - 如何在 Html angular5 中连接两个数组
- mysql - 用于选择没有与输入日期关联的预订的房间的 SQL 查询
- symfony - Symfony .env 生产模式下的安全密码
- c# - ASP.NET Core 自定义标签助手不起作用