📄 pex6_10.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "random.h"
#include "wex6_13.h"
// draw five numbers in the range 0..4. if they are
// distinct, return 1 (True); otherwise, return 0 (False)
int FillSet(void)
{
// set that contains numbers 0..4
Set S;
// static so one random sequence used
static RandomNumber rnd;
// insert 5 integers in range 0..4 into the set S
for(int i=0;i < 5;i++)
S.Insert(rnd.Random(5));
// break out of the loop if any of 0..4 is not
// in set S
for(i=0;i < 5;i++)
if ((i^S) == 0)
break;
// the numbers are distinct if we did not break out
// of the loop
return i == 5;
}
void main(void)
{
// perform the experiment tries times
const long tries = 100000L;
long i, count = 0;
// call FillSet tries times and count the number of
// successes
for(i=0;i < tries;i++)
if (FillSet())
count++;
// fixed point output, show exactly 4 decimal places
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);
// the simulated probablilty = #successes/#trials
cout << "The simulated probability of filling the set in five tries is ";
cout << double(count)/tries << endl;
}
/*
<Run>
The simulated probability of filling the set in five tries is 0.0385
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -