zoukankan      html  css  js  c++  java
  • [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

    赌徒赢得机会有多大?

    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);
        }
    }

    运行结果

    
    
  • 相关阅读:
    二叉树的遍历
    98验证二叉搜索树
    104二叉树的最大深度
    101对称二叉树
    100相同的树
    递归算法
    52N皇后II
    51N皇后
    90子集II
    526优美的排列
  • 原文地址:https://www.cnblogs.com/learning-c/p/5202372.html
Copyright © 2011-2022 走看看