1、题目:Compute with dartpoint randomly 用随机投点法计算 值程序步骤:创建头文件 randomnumber.h #ifndef RANDOMNUMBER_H#define RANDOMNUMBER_H#if _MSC_VER1000#pragma once#endif#include/随机数类const unsigned long maxshort = 65536L;const unsigned long multiplier = 1194211693L;const unsigned long adder = 12345L;class RandomNumberpr
2、ivate:/当前种子unsigned long randSeed;public:/构造函数,默认值 0 表示由系统自动产生种子RandomNumber(unsigned long s = 0);/产生 0:n-1 之间的随机整数unsigned short Random(unsigned long n);/产生0,1)之间的随机实数double fRandom(void);/产生种子RandomNumber:RandomNumber(unsigned long s)if (s = 0)randSeed = time(0); /用系统时间产生种子else randSeed = s; /由用户提
3、供种子/产生 0:n-1 之间的随机整数unsigned short RandomNumber:Random(unsigned long n)randSeed = multiplier * randSeed + adder;return (unsigned short)(randSeed 16) % n);/产生0,1) 之间的随机实数double RandomNumber:fRandom(void)return Random(maxshort) / double (maxshort);#endif编写源程序 random.cpp/ random.cpp : Defines the entry point for the console application./#include “stdafx.h“#include “randomnumber.h“#includeusing namespace std;/ Compute with dartpoint randomlyint main(int argc, char* argv)RandomNumber dart;int i=0,k=0; /落入圆内的点数为 kint n=10000; /向正方形随机地投掷 10000 个点double x,y;for(i=1;ii; /防止 exe 一闪而过return 0;结果显示: