[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);
}
}
运行结果
[Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟的更多相关文章
- [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.整数的素因子分解是乘 ...
- [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. ...
- Thinking in Java——笔记(1)
Introduction To Obejct The progress of abstraction But their primary abstraction still requires you ...
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- 每天一本电子书 - JavaScript for Kids: A Playful Introduction to Programming
JavaScript for Kids: A Playful Introduction to Programming 作者: Nick Morgan 出版社: No Starch Press 副标题 ...
- 2018-12-09 疑似bug_中文代码示例之Programming in Scala笔记第九十章
续前文: 中文代码示例之Programming in Scala笔记第七八章 源文档库: program-in-chinese/Programming_in_Scala_study_notes_zh ...
- 2018-11-27 中文代码示例之Programming in Scala笔记第七八章
续前文: 中文代码示例之Programming in Scala学习笔记第二三章 中文代码示例之Programming in Scala笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...
随机推荐
- 排序算法_HeapSort
大根堆排序的基本思想: 1) 先将初始文件R[1..n]建成一个大根堆,此堆为初始的无序区; 2) 再将关键字最大的记录R[1](即堆顶)和无序区的最后一个记录R[n]交换, 由此得到新的无序区 ...
- Bzoj 4196: [Noi2015]软件包管理器 树链剖分
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 721 Solved: 419[Submit][Statu ...
- DIY常用网站
工作: 技术: 学习: 个人十佳博客介绍:http://hedengcheng.com/?p=676
- ARCGIS 点抽稀的实现
点抽稀是制图综合的一部分,即在点数据点特别密集是,需要选取比较重要的点而舍去不要重的点,使得图面整洁. 一.基本原理 先把所有的点符号都设成透明.这样显示是没有点的.然后给这些点设置标注.标注方法很奇 ...
- Javascript之<script>标签
把javascript代码插入到HTML页面中需要使用<script>标签,使用这个元素可以使javascript和html标记混合在一个页面中,也可以引入外部的javascript文件. ...
- PHP开发APP接口(一)
php以json或者xml 形式返回给app.明白这点就很好说了,就是把数据包装成json或者xml,返回给APP 定义抽象APP基类: <?php /** * 定义API抽象类 */ abst ...
- wxPython中添加窗口标题图标
这种添加方法可以避免要将应用程序和图标放在同一个目录,可以实现从模块中读取图标 #用于从module中读取ico,避免了要在程序所在路径附上此ico exeName = win32api.GetMod ...
- use SWF / Flash in cocos2d-x; cocos2d(cocos2d-x) 直接播放flash / SWF文件
前段时间移植一个页游到手游,原先页游的项目里面有1000+的Flash人物,宠物动画,特效. 这要是全部重新做一遍,还不累死人?所以就想干脆直接在Cocos2d(x)里面播放SWF文件.(包括场景,过 ...
- JavaScript中childNodes、children、nodeValue、nodeType、parentNode、nextSibling详细讲解
其中属性.元素(标签).文本都属于节点 <title></title> <scripttype="text/javascript"> windo ...
- 介绍 JSON
出自官网:http://www.json.org/json-zh.html JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于 ...