2011年计算机二级C++实例编程辅导2_第4页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2011年11月10日

  double RandomNumber::fRandom()

  {

  return Random(maxshort) / double(maxshort);

  }

  主文件Main :

  代码

  // 主文件main

  /*

  * Author: Tanky woo

  * Blog: www.WuTianQi.com

  * Date: 2010.12.7

  * 代码来至王晓东《计算机算法设计与分析》

  */

  #include "RandomNumber.h"

  #include

  #include

  #include

  using namespace std;

  int TossCoins(int numberCoins)

  {

  // 随机抛硬币

  static RandomNumber coinToss;

  int i, tosses = 0;

  for(i = 0; i < numberCoins; ++i)

  tosses += coinToss.Random(2);

  return tosses;

  }

  int main()

  {

  // 模拟随机抛硬币事件

  const int NCOINS = 10;

  const long NTOSSES = 50000L;

  // heads[i]得到的i次正面的次数

  long i, heads[NCOINS+1];

  int j, position;

  // 初始化数组heads

  for(j = 0; j < NCOINS+1; ++j)

  heads[j] = 0;


相关文章