当前位置:网站首页>随机数(random头文件)
随机数(random头文件)
2022-07-20 05:42:00 【疯疯癫癫才自由】
#include <iostream>
#include <random>
using namespace std;
int main()
{
default_random_engine e;
for(int j=0;j<3;++j)
{
for(int i=0;i<10;++i)
cout << e() << ' ';
cout << endl;
}
uniform_int_distribution<unsigned> u(0,18);
for(int i=0;i<10;++i)
cout << u(e) << ' ';
cout << endl;
default_random_engine e2(12345);
for(int i=0;i<10;++i)
cout << u(e2) << ' ';
cout << endl << u.min() << ' ' << u.max();
cout << "\nHello world!" << endl;
return 0;
}
题目:
1)连续掷n次硬币,正反面概率相同,则一次正面朝上的概率为多少?
2)连续掷n次硬币,正面朝上的概率为2/3,则三次正面朝上的的概率为多少?
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
cout << 32.0/81 << endl;
default_random_engine e(time(0));//初始化随机数,使得前面给出的随机数不会影响
for(int i=0;i<10;++i) //后面还未给出的随机数
cout << e() <<' ';
cout << endl;
uniform_int_distribution<> u(0,1);
for(int i=0;i<10;++i)
cout << u(e) << ' ';
cout << endl;
default_random_engine e2;
for(int i=0;i<10;++i)
cout << u(e2) << ' ';
cout << endl;
cout << "Hello world!\n\n" << endl;
int total,n;
printf("输入掷硬币的次数以及每一次要连续掷多少次:\n");
cin >> total >> n;
int ans=0,x;
double result=0;
for(int i=0;i<total;++i)
{
u.reset();
int temp=0;
for(int j=0;j<n;++j)
{
x=u(e);
if(x==1)
++temp;
}
if(temp==1)
++ans;
}
result=(ans*1.0)/total;
printf("连续掷%d次硬币,正反面概率相同,则一次正面朝上的概率为:\n",n);
cout << result << endl;
uniform_real_distribution<> f(0,1.5); //正面朝上的概率为2/3
printf("\n输入掷硬币的次数以及每一次要连续掷多少次:\n");
cin >> total >> n;
ans=0;
double d;
result=0;
for(int i=0;i<total;++i)
{
f.reset();
int temp=0;
for(int j=0;j<n;++j)
{
d=f(e);
if(d>0.5)
++temp;
}
if(temp==3)
++ans;
}
result=(ans*1.0)/total;
printf("连续掷%d次硬币,正面朝上的概率为2/3,则三次正面朝上的的概率为:\n",n);
cout << result << endl;
return 0;
}
边栏推荐
猜你喜欢
香蕉派 BPI-M5折腾记录(2)—— 编译u-boot
解决tensoflow2.x的No module named:tensorflow.contrib
STM32CubeMX的正交编码器encoder
uni-app打包后不发请求的原因
Given a positive integer n, it is expressed as the addition of numbers 1,3,7,15. Please code to find the combination that minimizes the total number of occurrences of the above numbers (each number ca
城市轨道交通工程毕业论文题目
交通安全管理毕业论文范文
The new version of WordPress 6.0.1 has been released, and it is recommended to update it all.
WordPress opens sitemap XML, a piece of code to solve.
Topic of graduation thesis on Urban Rail Transit Engineering
随机推荐
结合源码看《我所理解的cocos2dx-3.0》—— 渲染流程
STM32CubeMX的Flash读写问题
二、mysql进程之间关系、配置文件、文件socket、网络socket、mysql密码破解
解决错误:Error: TomEE required to support EAR/EJB deployment
剑指 Offer II 016. 不含重复字符的最长子字符串
Model graduation thesis of Construction Engineering Surveying and mapping
香蕉派 BPI-M5折腾记录(2)—— 编译u-boot
电力系统及其自动化毕业论文题目【精选】
uni-app打包后不发请求的原因
WordPress 6.0.1 新版已经发布,建议全部更新。
[Unity脚本优化] Unity magic methods
聪明人的游戏提高篇:第三章第一课:isbn (ISBN码)
请教一下,如果我同步的是mysql binlog的数据 并且没有加特定的日志,我怎么查看当前程序同步
uni-app - App 平台内嵌网页物理手机自带返回键失效解决方案(内嵌的 webview 网页 H5 打包后手机物理返回键无效直接退出应用了)
STM32CubeMonitor的使用第二部分-历史数据存储以及网络访问
Unity2D 自定义Scriptable Tiles的理解与使用(一)——了解TileBase类
Chapter004-FPGA学习之IP核相关功能【时钟、RAM、FIFO】
php记录
UGUI官方优化文档翻译
mark down学习