打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用Java中随机生成的数据进行pi的Monte Carlo计算

我正在开发一个程序,该程序根据表示图表上x,y坐标的随机生成的浮点数来计算pi.每个x,y坐标乘以2的幂,然后存储在两个单独的数组中.坐标均匀地分布在间隔为0.1的图形上.

程序将x,y坐标相加,如果它们小于1,则这些点位于直径1的圆内,如下图所示.

然后我用公式

π≈4 w / n

制定圆周率.其中,w是圆内点的计数,n是数组内x或y坐标的数量.

当我将n设置为最大10,000,000(数组的大小)时,它将生成最精确的pi(15-16位小数位)计算.但是,在将4GB RAM分配给运行配置并将n设置为100,000,000 pi后,最终为0.6710 …

我想知道为什么会这样吗?抱歉,这是一个愚蠢的问题.代码在下面.

import java.text.DecimalFormat;import java.util.Random;public class random_pi {    public random_pi() {        float x2_store[] = new float[10000000];        float y2_store[] = new float[10000000];        float w = 0;        Random rand = new Random();        DecimalFormat df2 = new DecimalFormat("#,###,###");        for (int i = 0; i < x2_store.length; i  ) {            float x2 = (float) Math.pow(rand.nextFloat(), 2);            x2_store[i] = x2;            float y2 = (float) Math.pow(rand.nextFloat(), 2);            y2_store[i] = y2;        }        for (int i = 0; i < x2_store.length; i  ) {            if (x2_store[i]   y2_store[i] < 1) {                w  ;            }        }        System.out.println("w: " w);        float numerator = (4*w);        System.out.printf("4*w: "   (numerator));        System.out.println("\nn: "   df2.format(x2_store.length));        float pi = numerator / x2_store.length;        String fmt = String.format("%.20f", pi);        System.out.println(fmt);        String pi_string = Double.toString(Math.abs(pi));        int intP = pi_string.indexOf('.');        int decP = pi_string.length() - intP - 1;        System.out.println("decimal places: "   decP);    }    public static void main(String[] args) {        new random_pi();    }}

解决方法:

问题在这里:

float w = 0;float numerator = (4*w);

浮点精度不够,请将其更改为int或double:

像这样的工作示例代码:

import java.text.DecimalFormat;import java.util.Random;public class random_pi {    public random_pi() {        float x2_store[] = new float[100000000];        float y2_store[] = new float[100000000];        int w = 0;        Random rand = new Random();        DecimalFormat df2 = new DecimalFormat("#,###,###");        for (int i = 0; i < x2_store.length; i  ) {            float x2 = (float) Math.pow(rand.nextFloat(), 2);            x2_store[i] = x2;            float y2 = (float) Math.pow(rand.nextFloat(), 2);            y2_store[i] = y2;        }        for (int i = 0; i < x2_store.length; i  ) {            if (x2_store[i]   y2_store[i] < 1) {                w  ;            }        }        System.out.println("w: " w);        int numerator = (4*w);        System.out.printf("4*w: "   (numerator));        System.out.println("\nn: "   df2.format(x2_store.length));        float pi = ((float)numerator) / x2_store.length;        String fmt = String.format("%.20f", pi);        System.out.println(fmt);        String pi_string = Double.toString(Math.abs(pi));        int intP = pi_string.indexOf('.');        int decP = pi_string.length() - intP - 1;        System.out.println("decimal places: "   decP);    }    public static void main(String[] args) {        new random_pi();    }}

输出:

w: 785440414*w: 314176164n: 100,000,0003.14176154136657700000decimal places: 15

而且您不需要存储结果,例如以下工作示例代码:

import java.text.DecimalFormat;import java.util.Random;public class pi {    public pi() {        double n=100000000;         double w = 0;        Random rand = new Random();        DecimalFormat df2 = new DecimalFormat("#,###,###");        for (int i = 0; i < n; i  ) {            double x = rand.nextFloat();             double y = rand.nextFloat();              if ((x*x   y*y) < 1.0) w  ;        }        System.out.println("w: " w);//w: 7852372.0        double numerator = (4*w);        System.out.printf("4*w: "   (numerator));//4*w: 3.1409488E7        System.out.println("\nn: "   df2.format(n));//n: 10,000,000        double pi = numerator / n;        final String fmt = String.format("%.20f", pi);        System.out.println(fmt);//3.14094877243042000000        String pi_string = Double.toString(Math.abs(pi));        int intP = pi_string.indexOf('.');        int decP = pi_string.length() - intP - 1;        System.out.println("decimal places: "   decP);//decimal places: 14    }    public static void main(String[] args) {        new random_pi();    }}

输出:

w: 785396064*w: 314158424n: 100,000,0003.14158439636230470000decimal places: 16
来源:https://www.icode9.com/content-1-562351.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
蒙特卡洛算法
随机生成5位大小写字母或者数字
String类相关面试题很难?不要方,本文将让你彻底明白!
java输出颜色代码
Java DecimalFormat用法--小数格式化的0、#的区别
double数字除法与格式化 百分比等
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服