赶火车问题

  • 格式:docx
  • 大小:106.25 KB
  • 文档页数:6

赶火车问题仿真
报告

学号:100611117
学生:杨奎
班级:100613
院系:计算机科学与工程学院


时间:2013.4.26
赶火车问题
一:问题描述
一列火车从A站经过B站开往C站, 某人每天赶往B站乘这趟
火车去C站,但是由于货车出发时间和运行时间以及人在赶往B火车
站的时间都具有一定的随机性,所以下面将给出一些统计数据,你将
用计算机仿真来模拟此过程.
二:问题分析
已知火车从A站到B站运行时间为均值30分钟、标准差为2分
钟的正态随机变量。火车大约在下午1点离开A站。离开时刻的频率
分布为.
出发时刻(T) 1:00 1:05 1:10

频率 0.7 0.2 0.1

这个人到达B站时刻的频率分布为:

记此人赶上火车事件A:
A1 此人1:28到达B站赶上火车,
A2 此人1:30到达B站赶上火车,
A3 此人1:32到达B站赶上火车,
A4 此人1:34到达B站赶上火车,
事件 A1
火车1:00 从A站开出且1:28之后到达B站,
火车1:05 从A站开出且1:28之后到达B站,
火车1:10 从A站开出且1:28之后到达B站,
T1 火车从A站开出的时刻;
T2 火车从A站运行到B站所需要的时间;
T3 此人到达B站的时刻;
此人能赶上火车的条件是 T3< T1+T2
三:源代码
package com.yk01;
import java.util.Random;
import java.util.Scanner;
public class ByTrain11 {
public static void main(String[] args) {
int PepArride_time=0;
int TrainGo_time=0 ;
int RunTime=0;
int count=0;
// 人到达时间方法
System.out.println("请输入模拟人乘火车次数:");
Scanner scann = new Scanner(System.in);
int times=scann.nextInt();
for (int i = 0; i < times; i++) {
int go = (int) (Math.random() * 10 + 1);
int pepArride = (int) (Math.random() * 10 + 1);

if (pepArride >= 0 && pepArride <= 3) {
PepArride_time = 28;
} else if (pepArride > 3 && pepArride <= 7) {
PepArride_time = 30;
} else if (pepArride > 7 && pepArride <= 9) {
PepArride_time = 32;
} else if (pepArride > 9 && pepArride <= 10) {
PepArride_time = 34;
}
// 火车出发时间产生方法
if (go >= 0 && go <= 7) {
TrainGo_time = 0;
} else if (go > 7 && go <= 9) {
TrainGo_time = 5;
} else if (go > 9 && go <= 10) {
TrainGo_time = 10;
}

//火车运行时间产生方法,符合正态分布(30,2);
Random random=new Random();
RunTime=(int) ((random.nextGaussian()*Math.sqrt(2))+30);
if ((TrainGo_time+RunTime)>=PepArride_time) {
System.out.println("火车出发时间为:1:"+TrainGo_time+"
分\t火车运行时间为:"+RunTime+"分\t人到达时间:1:"+PepArride_time+"
分\t能赶上火车");
count=count+1;
System.out.println(count);
}else {
System.out.println("火车出发时间为:1:"+TrainGo_time+"
分\t火车运行时间为:"+RunTime+"分\t人到达时间:1:"+PepArride_time+"
分\t不能赶上火车");
}

}
System.out.println("此人赶上火车的概率为:" +
((double)count/(times)));
}

}
四:运行截图
当模拟100次时,能赶上火车概率为:0.67

当模拟1000次时,赶上火车概率为:0.659
当模拟100000次时,赶上火车概率:
五:结果分析与心得体会
在此赶火车问题中,由于火车运行时间为一个符合正太分布(30,2)
的随机分布,在产生时用到了
(random.nextGaussian()*Math.sqrt(2))+30)这个方法,而
且当模拟次数不断增加时,其赶上火车的概率靠近0.63.
通过此计算机仿真,让自己更熟悉仿真的这个过程,而且对自己
的代码编写能力也有一定的提高,是一个不错学习过程。