瑞利分布的概率密度函数为
瑞丽分布的均值为,方差为
首先使用逆变换法产生参数的指数分布的随机变量
,其概率密度函数为
然后通过变换,产生瑞丽分布的随机变量x,具体的方法如下:
(1)产生均匀分布的随机数
(2)计算
(3)计算
1、头文件 rayleigh.h
#pragma once
#ifndef RAYLEIGH_H_
#define RAYLEIGH_H_/*
函数功能: 瑞利分布的随机数
输入参数说明:
sigma 瑞利分布的均值
seed 长整型指针变量, *seed 为伪随机数的种子
*/double rayleigh_data(double sigma, long int * seed);#endif // !RAYLEIGH_H
2、rayleigh.c
#include "rayleigh.h"#include <stdint.h>
#include "string.h"
#include "stdio.h"
#include "uniform.h"
#include <math.h>double rayleigh_data(double sigma, long int * seed)
{double u, x;u = uniform_data(0.0, 1.0, seed);x = -2.0 * log(u);x = sigma + sqrt(x);return x;
}
3、主函数 main.c
#include <stdio.h>
#include <string.h>
#include "uniform.h"
#include "rayleigh.h"int main()
{// 产生50个瑞利分布的随机数int i, j;long int s;double x, sigma;sigma = 1.0; s = 13579;for (i = 0; i < 10; i++){for (j = 0; j < 5; j++){x = rayleigh_data(sigma, &s);printf("%13.7f",x);}printf("\n");} getchar(); // 此行代码是为了保持输出窗口,按任意按键关闭弹窗return 0;
}
4、调试输出结果