C++ 程序,使用概率分布函数生成随机数
概率密度函数 (pdf) 是一种函数,用于描述该随机变量取得给定值时的相对可能性。它也称为连续随机变量的密度。
随机变量落在特定值范围之内的概率由该变量在该范围上密度的积分给出,因此,它由密度函数下方的区域给出,但在水平轴上方,并介于范围的最小值和最大值之间。概率分布基于此概率密度函数。
算法
Begin
Declare n
Assign pdf=0
For i =0 to n , do
pdf = rand() mod 200
If pdf greater than 360
Print 1
Else if pdf less than 0
Print 0
Else
Print pdf * 0.1 / 360
Done
Done
end示例代码
#include <iostream>
using namespace std;
int n = 6;
int main(int argc, char **argv) {
int pdf = 0;
for (int i = 0; i < n; i++) {
pdf = rand() % 200;
if (pdf > 360)
cout << 1 << " ";
else if (pdf < 0)
cout << 0 << " ";
else
cout << pdf * 0.1 / 360 << " ";
}
cout << "...";
}输出
0.0508333 0.0238889 0.0491667 0.0319444 0.0536111 0.0375 ...
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP