题目:随机数函数a,产生0的概率0.7,1的概率0.3。怎样用该随机函数生成一个随机函数b,使得b产生0和1的概率为0.5
代码实现如下:
#include<iostream>
using namespace std;
int main() {
int alpha = 0;
int beta = 0;
int total = 0;
for (int t = 0; t<10000; t++)
{
int small = 0;
int big = 0;
for (int i = 0; i<10000; i++)
{
int val = rand() % 10;
if (val<3) small++;
else big++;
}
if (big<7000) alpha++;
else if (big>7000) beta++;
if (big != 7000) total++;
}
cout << (float)alpha / (float)total << ' ' << (float)beta / (float)total << ' ' << (float)alpha / (float)total - (float)beta / (float)total << endl;
return 0;
}