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;