1. 程式人生 > >數字訊號產生之瑞利分佈的隨機數

數字訊號產生之瑞利分佈的隨機數

uniform.h

#pragma once

class uniform
{
private:
 double a, b, generate_num;
 int * seed;
 int s;
 int M, N, i, j;

public:
 uniform()
 {
  M = 1048576;
  N = 2045;
 }
 void generate();
 double random_number(double, double, int *);
};

double uniform::random_number(double a, double b, int * seed)
{
 (*seed) = N * (*seed) + 1;
 (*seed) = (*seed) - ((*seed) / M) * M;
 generate_num = static_cast<double>((*seed)) / M;
 generate_num = a + (b - a) * generate_num;
 return (generate_num);
}

rayleigh.h

#pragma once
#include <math.h>
#include "uniform.h"

class rayleigh
{
private:
 double sigma, u, x, generate_num;
 int * seed;
 int s, i, j;

public:
 rayleigh() {}
 void generate();
 double random_number(double, int *);
};

double rayleigh::random_number(double sigma, int * seed)
{
 uniform unif_num;
 u = unif_num.random_number(0.0, 1.0, seed);
 x = -2.0 * log(u);
 x = sigma * sqrt(x);
 return (x);
}

rayleigh.cpp

//產生50個引數sigma = 1的瑞利分佈的隨機數
#include <iostream>
#include <iomanip>
#include "Rayleigh.h"

using namespace std;

void main()
{
 rayleigh solution;
 solution.generate();
}

void rayleigh::generate()
{
 cout << "輸入瑞利分佈的引數:";
 cin >> sigma;
 cout << "輸入隨機數的種子:";
 cin >> s;
 cout << "生成的隨機數結果為:" << endl;
 for (i = 0; i < 10; i++)
 {
  for (j = 0; j < 5; j++)
  {
   generate_num = random_number(sigma, &s);
   cout << setw(10) << generate_num;
  }
  cout << endl;
 }
}