首页 / 算法 / [算法] 随机模拟问题
[算法] 随机模拟问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[算法] 随机模拟问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含12174字,纯文字阅读大概需要18分钟。
内容图文
![[算法] 随机模拟问题](/upload/InfoBanner/zyjiaocheng/1162/d289123ec8414e339f197d7804fc8437.jpg)
分钱问题
- 100个人,初始每人100块钱,每轮每人将1元随机分给另一个人,若干轮过后,100个人的财富分布情况会怎样
- 画布的原点在左上角,y轴向下
AlgoVisHelper.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.EventQueue; 2 import java.util.Arrays; 3 4 public class AlgoVisualizer { 5 6 private static int DELAY = 40; 7privateint[] money; 8private AlgoFrame frame; 910public AlgoVisualizer(int sceneWidth, int sceneHeight) { 1112// 初始化数据13 money = newint[100]; 14for(int i = 0 ; i < money.length ; i ++) { 15 money[i] = 100; 16 } 1718// 初始化视图19 EventQueue.invokeLater(()->{ 20 frame = new AlgoFrame("Welcome",sceneWidth,sceneHeight); 21new Thread(()->{ 22 run(); 23 }).start(); 24 }); 25 } 2627privatevoid run() { 28while(true) { 29 Arrays.sort(money); 30 frame.render(money); 31 AlgoVisHelper.pause(DELAY); 3233for(int k = 0 ; k < 50 ; k ++ ) { 34for(int i = 0 ; i < money.length ; i ++ ) { 35int j = (int)(Math.random() * money.length); 36 money[i] -= 1; 37 money[j] += 1; 38 } 39 } 40 } 41 } 4243publicstaticvoid main(String[] args) { 4445int sceneWidth = 1000; 46int sceneHeight = 800; 4748 AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight); 4950 } 51 }
AlgoVisualizer.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.EventQueue; 2 import java.util.Arrays; 3 4 public class AlgoVisualizer { 5 6 private static int DELAY = 40; 7privateint[] money; 8private AlgoFrame frame; 910public AlgoVisualizer(int sceneWidth, int sceneHeight) { 1112// 初始化数据13 money = newint[100]; 14for(int i = 0 ; i < money.length ; i ++) { 15 money[i] = 100; 16 } 1718// 初始化视图19 EventQueue.invokeLater(()->{ 20 frame = new AlgoFrame("Welcome",sceneWidth,sceneHeight); 21new Thread(()->{ 22 run(); 23 }).start(); 24 }); 25 } 2627privatevoid run() { 28while(true) { 29 Arrays.sort(money); 30 frame.render(money); 31 AlgoVisHelper.pause(DELAY); 3233for(int k = 0 ; k < 50 ; k ++ ) { 34for(int i = 0 ; i < money.length ; i ++ ) { 35int j = (int)(Math.random() * money.length); 36 money[i] -= 1; 37 money[j] += 1; 38 } 39 } 40 } 41 } 4243publicstaticvoid main(String[] args) { 4445int sceneWidth = 1000; 46int sceneHeight = 800; 4748 AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight); 4950 } 51 }
AlgoFrame.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.Color; 2 import java.awt.Dimension; 3 import java.awt.Graphics; 4 import java.awt.Graphics2D; 5 import java.awt.RenderingHints; 6 7 import javax.swing.JFrame; 8 import javax.swing.JPanel; 9 10 public class AlgoFrame extends JFrame{ 1112privateint canvasWidth; 13privateint canvasHeight; 1415public AlgoFrame(String title, int canvasWidth, int canvasHeight) { 1617super(title); 1819this.canvasWidth = canvasWidth; 20this.canvasHeight = canvasHeight; 2122 AlgoCanvas canvas = new AlgoCanvas(); 23 setContentPane(canvas); 24 pack(); 2526 setResizable(false); 27 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 setVisible(true); 29 } 3031public AlgoFrame(String title) { 32this(title, 1024, 768); 33 } 3435publicint getCanvasWidth() {return canvasWidth;} 36publicint getCanvasHeight() {return canvasHeight;} 3738privateint[] money; 39publicvoid render(int[] money) { 40this.money = money; 41 repaint(); 42 } 4344privateclass AlgoCanvas extends JPanel{ 4546public AlgoCanvas() { 47super(true); 48 } 4950 @Override 51publicvoid paintComponent(Graphics g) { 5253super.paintComponent(g); 5455 Graphics2D g2d = (Graphics2D)g; 5657//抗锯齿58 RenderingHints hints = new RenderingHints( 59 RenderingHints.KEY_ANTIALIASING, 60 RenderingHints.VALUE_ANTIALIAS_ON); 61 g2d.addRenderingHints(hints); 6263//具体绘制64int w = canvasWidth / money.length; 65for(int i = 0 ; i < money.length ; i ++ ) { 66if(money[i] > 0) { 67 AlgoVisHelper.setColor(g2d, AlgoVisHelper.Blue); 68 AlgoVisHelper.fillRectangle(g2d, 69 i*w + 1, canvasHeight/2 - money[i], w-1, money[i]); 70 } 71elseif(money[i] < 0) { 72 AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red); 73 AlgoVisHelper.fillRectangle(g2d, 74 i*w + 1, canvasHeight/2, w-1, -money[i]); 75 } 76 } 77 } 7879 @Override 80public Dimension getPreferredSize() { 81returnnew Dimension(canvasWidth, canvasHeight); 82 } 83 } 84 }
蒙特卡洛方法
- 为解决裂变物质的中子随机扩散问题而提出
- 通过大量随机样本,了解一个系统,进而得到所要计算的值
- 红色点的数量-->圆的面积;红色点+绿色点的数量-->方的面积
- PI = 4 * 红色点数 / 总点数
AlgoFrame.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.Color; 2 import java.awt.Dimension; 3 import java.awt.Graphics; 4 import java.awt.Graphics2D; 5 import java.awt.Point; 6 import java.awt.RenderingHints; 7 import java.util.LinkedList; 8 9 import javax.swing.JFrame; 10 import javax.swing.JPanel; 11 12 public class AlgoFrame extends JFrame{ 13privateint canvasWidth; 14privateint canvasHeight; 1516public AlgoFrame(String title, int canvasWidth, int canvasHeight) { 17super(title); 1819this.canvasWidth = canvasWidth; 20this.canvasHeight = canvasHeight; 2122 AlgoCanvas canvas = new AlgoCanvas(); 23 setContentPane(canvas); 24 pack(); 2526 setResizable(false); 27 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 setVisible(true); 29 } 3031public AlgoFrame(String title) { 32this(title, 1024, 768); 33 } 3435publicint getCanvasWidth() {return canvasWidth;} 36publicint getCanvasHeight() {return canvasHeight;} 3738private MonteCarloPiaData data; 39publicvoid render(MonteCarloPiaData data) { 40this.data = data; 41 repaint(); 42 } 4344privateclass AlgoCanvas extends JPanel{ 4546public AlgoCanvas() { 47super(true); 48 } 4950 @Override 51publicvoid paintComponent(Graphics g) { 5253super.paintComponent(g); 5455 Graphics2D g2d = (Graphics2D)g; 5657//抗锯齿58 RenderingHints hints = new RenderingHints( 59 RenderingHints.KEY_ANTIALIASING, 60 RenderingHints.VALUE_ANTIALIAS_ON); 61 g2d.addRenderingHints(hints); 6263//具体绘制64 Circle circle = data.getCircle(); 65 AlgoVisHelper.setStrokeWidth(g2d, 3); 66 AlgoVisHelper.setColor(g2d, AlgoVisHelper.Blue); 67 AlgoVisHelper.strokeCircle(g2d, circle.getX(), circle.getY(), circle.getR()); 6869for(int i = 0 ; i < data.getPointsNumber() ; i ++ ) { 70 Point p = data.getPoint(i); 71if(circle.contain(p)) 72 AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red); 73else74 AlgoVisHelper.setColor(g2d, AlgoVisHelper.Green); 75 AlgoVisHelper.fillCircle(g2d, p.x, p.y, 3); 76 } 77 } 7879 @Override 80public Dimension getPreferredSize() { 81returnnew Dimension(canvasWidth, canvasHeight); 82 } 83 } 84 }
AlgoVisualizer.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.EventQueue; 2 import java.awt.Point; 3 import java.util.LinkedList; 4 5 public class AlgoVisualizer { 6 7 private static int DELAY = 40; 8 9private MonteCarloPiaData data; 10private AlgoFrame frame; 11privateint N; 1213public AlgoVisualizer(int sceneWidth, int sceneHeight, int N) { 1415if(sceneWidth != sceneHeight) 16thrownew IllegalArgumentException("This demo must be run in a square"); 17// 初始化数据18this.N = N; 19 Circle circle = new Circle(sceneWidth/2, sceneHeight/2, sceneWidth/2); 20 data = new MonteCarloPiaData(circle); 2122// 初始化视图23 EventQueue.invokeLater(()->{ 24 frame = new AlgoFrame("Get Pi with Monte Carlo",sceneWidth,sceneHeight); 25new Thread(()->{ 26 run(); 27 }).start(); 28 }); 29 } 3031// 动画逻辑32privatevoid run() { 33for(int i = 0 ; i < N ; i ++ ) { 3435if(i % 100 == 0) { 36 frame.render(data); 37 AlgoVisHelper.pause(DELAY); 38 System.out.println(data.estimatePi()); 39 } 4041int x = (int)(Math.random() * frame.getCanvasWidth()); 42int y = (int)(Math.random() * frame.getCanvasHeight()); 43 data.addPoint(new Point(x,y)); 44 } 45 } 4647publicstaticvoid main(String[] args) { 4849int sceneWidth = 800; 50int sceneHeight = 800; 51int N = 10000; 5253 AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N); 54 } 55 }
AlgoVisHelper.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.BasicStroke; 2 import java.awt.Color; 3 import java.awt.Graphics2D; 4 import java.awt.geom.Ellipse2D; 5 6 public class AlgoVisHelper { 7 public AlgoVisHelper() {} 8 9 public static final Color Red = new Color(0xF44336); 10publicstaticfinal Color Pink = new Color(0xE91E63); 11publicstaticfinal Color Purple = new Color(0x9C27B0); 12publicstaticfinal Color DeepPurple = new Color(0x673AB7); 13publicstaticfinal Color Indigo = new Color(0x3F51B5); 14publicstaticfinal Color Blue = new Color(0x2196F3); 15publicstaticfinal Color LightBlue = new Color(0x03A9F4); 16publicstaticfinal Color Cyan = new Color(0x00BCD4); 17publicstaticfinal Color Teal = new Color(0x009688); 18publicstaticfinal Color Green = new Color(0x4CAF50); 19publicstaticfinal Color LightGreen = new Color(0x8BC34A); 20publicstaticfinal Color Lime = new Color(0xCDDC39); 21publicstaticfinal Color Yellow = new Color(0xFFEB3B); 22publicstaticfinal Color Amber = new Color(0xFFC107); 23publicstaticfinal Color Orange = new Color(0xFF9800); 24publicstaticfinal Color DeepOrange = new Color(0xFF5722); 25publicstaticfinal Color Brown = new Color(0x795548); 26publicstaticfinal Color Grey = new Color(0x9E9E9E); 27publicstaticfinal Color BlueGrey = new Color(0x607D8B); 28publicstaticfinal Color Black = new Color(0x000000); 29publicstaticfinal Color White = new Color(0xFFFFFF); 3031publicstaticvoid setStrokeWidth(Graphics2D g2d, int w) { 32int strokeWidth = w; 33 g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)); 34 } 35publicstaticvoid setColor(Graphics2D g2d, Color color) { 36 g2d.setColor(color); 37 } 38publicstaticvoid strokeCircle(Graphics2D g2d, int x, int y, int r) { 39 Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r); 40 g2d.draw(circle); 41 } 42publicstaticvoid fillCircle(Graphics2D g2d, int x, int y, int r) { 43 Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r); 44 g2d.fill(circle); 45 } 46publicstaticvoid pause(int t) { 47try { 48 Thread.sleep(t); 49 } 50catch(InterruptedException e) { 51 System.out.println("Error in sleeping."); 52 } 53 } 54 }
Circle.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.Point; 2 3 public class Circle { 4 5 public int x, y, r; 6 7 public Circle(int x, int y, int r ) { 8this.x = x; 9this.y = y; 10this.r = r; 11 } 1213publicint getX() { return x; } 14publicint getY() { return y; } 15publicint getR() { return r; } 1617publicboolean contain(Point p) { 18return Math.pow(p.getX() - x, 2) + Math.pow(p.getY() - y, 2) <= r*r; 19 } 20 }
MonteCarloPiaData.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.Point; 2 import java.util.LinkedList; 3 4 public class MonteCarloPiaData { 5 6 private Circle circle; 7 private int insideCircle = 0; 8private LinkedList<Point> points; 910public MonteCarloPiaData(Circle circle) { 11this.circle = circle; 12 points = new LinkedList<Point>(); 13 } 1415public Circle getCircle() { 16return circle; 17 } 1819public Point getPoint(int i) { 20if(i < 0 || i >= points.size()) 21thrownew IllegalArgumentException("out of bound in getPoint!"); 22return points.get(i); 23 } 2425publicint getPointsNumber() { 26return points.size(); 27 } 2829publicvoid addPoint(Point p) { 30 points.add(p); 31if(circle.contain(p)) 32 insideCircle ++; 33 } 3435publicdouble estimatePi() { 36if(points.size() == 0) 37return 0.0; 38int circleArea = insideCircle; 39int squareArea = points.size(); 40return (double)circleArea*4 / squareArea; 41 } 42 }
控制台演示
MonteCarloExperiment.java
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053651988.jpg)
![技术分享图片](/upload/getfiles/default/2022/10/30/20221030053652273.jpg)
1 import java.awt.*; 2 3publicclass MonteCarloExperiment { 4 5privateint squareSide; 6privateint N; 7privateint outputInterval = 100; 8 9public MonteCarloExperiment(int squareSide, int N) { 10if(squareSide <= 0 || N <= 0) 11thrownew IllegalArgumentException("squareSide and N must larger than 0"); 12this.squareSide = squareSide; 13this.N = N; 14 } 1516publicvoid setOutputInterval(int interval) { 17if(interval <= 0) 18thrownew IllegalArgumentException("interval must be larger than 0"); 19this.outputInterval = interval; 20 } 2122publicvoid run() { 2324 Circle circle = new Circle(squareSide/2, squareSide/2, squareSide/2); 25 MonteCarloPiData data = new MonteCarloPiData(circle); 2627for(int i = 0 ; i < N ; i ++) { 28if( i % outputInterval == 0 ) 29 System.out.println(data.estimatePi()); 30int x = (int)(Math.random() * squareSide); 31int y = (int)(Math.random() * squareSide); 32 data.addPoint(new Point(x,y)); 33 } 34 } 3536publicstaticvoid main(String[] args) { 3738int squareSide = 800; 39int N = 1000000; 4041 MonteCarloExperiment exp = new MonteCarloExperiment(squareSide, N); 42 exp.setOutputInterval(10000); 43 exp.run(); 44 } 45 }
原文:https://www.cnblogs.com/cxc1357/p/12853746.html
内容总结
以上是互联网集市为您收集整理的[算法] 随机模拟问题全部内容,希望文章能够帮你解决[算法] 随机模拟问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。