[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笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...
随机推荐
- linux 双网关双IP设置
server:CentOS5.8 ip:172.16.8.11 Gateway:172.16.8.1 ip:10.120.6.78 Gateway:10.120.6.1 网卡配置: eth0 poin ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 拥挤距离计算 crowddist.c
/* Crowding distance computation routines */ # include <stdio.h> # include <stdlib.h> # ...
- getaccesstoken方法
通过appid和appsecret获取access_token的定义函数 这里用的是memcache缓存存储用户信息7000秒 <?php function getAccessToken($ap ...
- PAT 1080. Graduate Admission (30)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- Java编程性能优化一
转自:http://my.oschina.net/xianggao/blog/77224 在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著 ...
- Ajax 整理总结(入门)
Ajax 学习要点: 1.Ajax 概述 2.load()方法 3.$.get()和$.post() 4.$.getScript()和$.getJSON() 5.$.ajax()方法 6.表单序列化 ...
- GXT之旅:第三章:表单和窗口(4)——表单的提交和RPC
表单使用HTTP提交 表单有两种提交方式,第一种就是传统的HTTP提交. 最直接的步骤就是: 使用FormPanel的setAction()方法,去定义submit的URL 使用FormPanel的i ...
- c#常用工具类:文件和二进制转换
//================二进制相关转换类============== #region 将文件转换为二进制数组 /// <summary> /// 将文件转换为二进制数组 /// ...
- Gprinter Android SDK V2.1 使用说明
下载:http://download.csdn.net/download/abc564643122/8872249
- 【转】java 解析 plist文件
为了方便的将spritesheet的图导入我自己的动画编辑器!我做了plist文件解析DOM解析比较麻烦 因为element getChildNodes 会获取到text对象.而这个对象可能是一个空白 ...