首页 > 编程语言 > 详细

[Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

时间:2016-02-20 00:20:29      阅读:240      评论:0      收藏:0      [点我收藏+]

赌徒赢得机会有多大?

技术分享

public class Gambler
{
    public static void main(String[] args)
    {  // Run T experiments that start with $stake and terminate on $0 and $goal
       int stake = Integer.parseInt(args[0]);
       int goal  = Integer.parseInt(args[1]);
       int T     = Integer.parseInt(args[2]);
       int bets = 0;
       int wins = 0;
       for (int t = 0; t < T; t++)
       {   // Run one experiment
           int cash = stake;
           while(cash > 0 && cash < goal)
           {   // Simulate one bet.
               bets++;
               if (Math.random() < 0.5) cash++;
               else                     cash--;
           } // Cash is either 0 (ruin) or $goal (win)
           if (cash == goal) wins++;
       }
       System.out.println(100*wins/T + "% wins");
       System.out.println("Avg # bets: " + bets/T);
    }
}

运行结果

技术分享

 

 

[Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

原文:http://www.cnblogs.com/learning-c/p/5202372.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!