题目内容
给定一个函数f1,功能是生成1-5的随机整数(1、2、3、4、5)。但这个函数是黑盒,不知道里面内容是什么。要求编写一个函数,功能是生成1-7(1、2、3、4、5、6、7)的随机整数随机数。
分析
不能直接f2=f1*1.4。需要先用f1,生成0、1的随机数。然后利用二进制、位运算,等概率生成0-7。生成0时候return重新生成,即可等概率生成1-7。
代码
f0为随机生成1-5
int f0()
{
return (rand() % 5) + 1;
} //1--5
f1为随机生成0、1
int f1()
{
int num;
do
{
num = f0();
} while (num == 3);
return num < 3 ? 0 : 1;
} //0、1
f2为随机生成0-7
此处利用了位运算,<<为左移。
int f2()
{
return (f1() << 2) + (f1() << 1) + (f1() << 0);
}//0--7
f3为随机生成1-7
此处利用do while,当生成为0时,返回重新生成,从而等概率生成1-7。
int f3()
{
int num;
do
{
num = f2();
} while (num ==0);
return num;
}//1--7
之后为测试代码,在完整代码中查看
完整代码
//P14由1-5随机得到1-7随机
#include <iostream>
#include <ctime>
using namespace std;
int f0()
{
return (rand() % 5) + 1;
} //1--5
int f1()
{
int num;
do
{
num = f0();
} while (num == 3);
return num < 3 ? 0 : 1;
} //0、1
int f2()
{
return (f1() << 2) + (f1() << 1) + (f1() << 0);
}//0--7
int f3()
{
int num;
do
{
num = f2();
} while (num ==0);
return num;
}//1--7
int main()
{
int Test_times = 100000;
int counts_0 = 0;
int counts_1 = 0;
int counts_2 = 0;
int counts_3 = 0;
int counts_4 = 0;
int counts_5 = 0;
int counts_6 = 0;
int counts_7 = 0;
double p0 = 0;
double p1 = 0;
double p2 = 0;
double p3 = 0;
double p4 = 0;
double p5 = 0;
double p6 = 0;
double p7 = 0;
srand(static_cast <unsigned> (time(0)));
//for (int i = 0; i < Test_times; i++)
//{
// int anc=f1();
// if (f1() == 0)
// counts_0 += 1;
// if (f1() == 1)
// counts_1 += 1;
//}
//
//cout << "=================" << endl;
//cout << counts_0<<endl;
//cout << counts_1<<endl;
//
//for (int i = 0; i < Test_times; i++)
//{
// int anc = f2();
// if (f2() == 0)
// counts_0 += 1;
// if (f2() == 1)
// counts_1 += 1;
// if (f2() == 2)
// counts_2 += 1;
// if (f2() == 3)
// counts_3 += 1;
// if (f2() == 4)
// counts_4 += 1;
// if (f2() == 5)
// counts_5 += 1;
// if (f2() == 6)
// counts_6 += 1;
// if (f2() == 7)
// counts_7 += 1;
//
//}
//cout << "=================" << endl;
//cout << counts_0 << endl;
//cout << counts_1 << endl;
//cout << counts_2 << endl;
//cout << counts_3 << endl;
//cout << counts_4 << endl;
//cout << counts_5 << endl;
//cout << counts_6 << endl;
//cout << counts_7 << endl;
//p0 = double(counts_0) / Test_times;
//p1 = double(counts_1) / Test_times;
//p1 = double(counts_2) / Test_times;
//p1 = double(counts_3) / Test_times;
//p1 = double(counts_4) / Test_times;
//p1 = double(counts_5) / Test_times;
//p1 = double(counts_6) / Test_times;
//p1 = double(counts_7) / Test_times;
//printf("P(x==0)==%f\n", p0);
//printf("P(x==1)==%f\n", p1);
//printf("P(x==2)==%f\n", p2);
//printf("P(x==3)==%f\n", p3);
//printf("P(x==4)==%f\n", p4);
//printf("P(x==5)==%f\n", p5);
//printf("P(x==6)==%f\n", p6);
//printf("P(x==7)==%f\n", p7);
for (int i = 0; i < Test_times; i++)
{
int anc = f3();
if (f3() == 0)
counts_0 += 1;
if (f3() == 1)
counts_1 += 1;
if (f3() == 2)
counts_2 += 1;
if (f3() == 3)
counts_3 += 1;
if (f3() == 4)
counts_4 += 1;
if (f3() == 5)
counts_5 += 1;
if (f3() == 6)
counts_6 += 1;
if (f3() == 7)
counts_7 += 1;
}
cout << "=================" << endl;
cout << counts_0 << endl;
cout << counts_1 << endl;
cout << counts_2 << endl;
cout << counts_3 << endl;
cout << counts_4 << endl;
cout << counts_5 << endl;
cout << counts_6 << endl;
cout << counts_7 << endl;
p0 = double(counts_0) / Test_times;
p1 = double(counts_1) / Test_times;
p2 = double(counts_2) / Test_times;
p3 = double(counts_3) / Test_times;
p4 = double(counts_4) / Test_times;
p5 = double(counts_5) / Test_times;
p6 = double(counts_6) / Test_times;
p7= double(counts_7) / Test_times;
cout << "=================" << endl;
printf("P(x==0)==%f\n", p0);
printf("P(x==1)==%f\n", p1);
printf("P(x==2)==%f\n", p2);
printf("P(x==3)==%f\n", p3);
printf("P(x==4)==%f\n", p4);
printf("P(x==5)==%f\n", p5);
printf("P(x==6)==%f\n", p6);
printf("P(x==7)==%f\n", p7);
cout << "=================" << endl;
return 0;
}