[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笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...
随机推荐
- NIO的学习
参考 http://wenku.baidu.com/link?url=rq-BEp3Et4JRrE62f2Lv9hq8nT_Gq0XPb65h8OBqTAt-ILfqKmdjIhVEp8bctIdm0 ...
- attribute section的用法
1. gcc的__attribute__编译属性 要了解Linux Kernel代码的分段信息,需要了解一下gcc的__attribute__的编绎属性,__attribute__主要用于改变所声明或 ...
- Jsp_demo:自定义标签
Jsp自定义标签: 1.继承SimpleTagSupport,重写doTag(). 2.在WEB-INF/ 下配置**.tld文件 3.Jsp页面引入自定义标签:<%@ taglib uri=& ...
- 完美转换MySQL的字符集 Mysql 数据的导入导出,Mysql 4.1导入到4.0
MySQL从4.1版本开始才提出字符集的概念,所以对于MySQL4.0及其以下的版本,他们的字符集都是Latin1的,所以有时候需要对mysql的字符集进行一下转换,MySQL版本的升级.降级,特别是 ...
- Gradle DSL method found: ‘android()’错误
Gradle DSL method found: ‘android()’错误 和上个错误一样这个也是因为在新版本的Gradle中android()方法已经废弃,但是要注意android()只是在整个项 ...
- Genymotion常见问题汇总(转)
为什么说是常见问题整合呢,因为我就是Genymotion最悲剧的使用者,该见过的问题,我基本都见过了,在此总结出这血的教训,望大家不要重蹈覆辙. 常见问题1:Genymotion在开启模拟器时 ...
- 新安装XAMPP,phpMyAdmin错误:#1045 - Access denied for user 'root'@'localhost' (using password: NO)
错误如下: 打开D:\Program Files\xampp\phpMyAdmin(你的xampp的安装目录下的phpMyAdmin目录)目录下的config.inc.php文件 将 改为 然后,错误 ...
- CSS 列表 你知道吗
CSS 列表属性允许你放置.改变列表项标志,或者将图像作为列表项标志.CSS 列表从某种意义上讲,不是描述性的文本的任何内容都可以认为是列表.人口普查.太阳系.家谱.参观菜单,甚至你的所有朋友都可以表 ...
- SQL查询记录添加序号(HANA)
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) row_number() OVER() 从1开始,为每一条分组记录返回一个数字,这里 ...
- UIImage载入图片的几种方式及差别
用UIImage载入图像的方法非常多.最经常使用的是几种: 1.使用imageNamed函数载入: <span style="font-size:14px;">[UI ...