赌徒赢得机会有多大?

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 赌徒破产模拟的更多相关文章

  1. [Introduction to programming in Java 笔记] 1.3.9 Factoring integers 素因子分解

    素数 A prime is an integer greater than one whose only positive divisors are one and itself.整数的素因子分解是乘 ...

  2. [Introduction to programming in Java 笔记] 1.3.7 Converting to binary 十进制到二进制的转换

    public class Binary { public static void main(String[] args) { // Print binary representation of N. ...

  3. Thinking in Java——笔记(1)

    Introduction To Obejct The progress of abstraction But their primary abstraction still requires you ...

  4. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  5. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  6. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  7. 每天一本电子书 - JavaScript for Kids: A Playful Introduction to Programming

    JavaScript for Kids: A Playful Introduction to Programming 作者: Nick Morgan  出版社: No Starch Press 副标题 ...

  8. 2018-12-09 疑似bug_中文代码示例之Programming in Scala笔记第九十章

    续前文: 中文代码示例之Programming in Scala笔记第七八章 源文档库: program-in-chinese/Programming_in_Scala_study_notes_zh ...

  9. 2018-11-27 中文代码示例之Programming in Scala笔记第七八章

    续前文: 中文代码示例之Programming in Scala学习笔记第二三章 中文代码示例之Programming in Scala笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...

随机推荐

  1. JSP九大内置对象详解

    内置对象特点: 1.            由JSP规范提供,不用编写者实例化. 2.            通过Web容器实现和管理 3.            所有JSP页面均可使用 4.     ...

  2. C# config配置文件 自定义节点读取

    主要使用:ConfigurationSection IConfigurationSectionHandler已被否决 http://technet.microsoft.com/zh-cn/librar ...

  3. sqlmap win32下命令集合

    http://testphp.vulnweb.com/artists.php?artist=1    #库 注意:--前面有一个空格 C:\Python27\sqlmap>sqlmap.py - ...

  4. Dijkstra算法求解最短路径分析

    最短路径是图论算法中的经典问题.图分为有向图.无向图,路径权值有正值.负值,针对不同的情况需要分别选用不同的算法.在维基上面给出了各种不同的场景应用不同的算法的基本原则:最短路问题. 针对无向图,正权 ...

  5. java中进程与线程--三种实现方式

    一:进程与线程 概述:几乎任何的操作系统都支持运行多个任务,通常一个任务就是一个程序,而一个程序就是一个进程.当一个进程运行时,内部可能包括多个顺序执行流,每个顺序执行流就是一个线程. 进程:进程是指 ...

  6. Java中的Stringbuffer类解析

    StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存 ...

  7. jetty服务器

    1,http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin 2,http://wiki.eclipse.org/Jetty#Getting_S ...

  8. Bison executable not found in PATH by mysql install

    [root@luozhonghua mysql-5.5.21]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/d ...

  9. LeetCode——Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  10. Redis实战之Redis + Jedis[转]

    http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报   目录(?)[-] ...