Environment: qt5+vs2013 Compilation
One, evenly distributed
Double uniform (double a,double b,long int *seed)
A lower bound, Upper B, seed random seed.
Main.cpp
#include <QCoreApplication>#include<math.h>#include<stdio.h>#include<stdlib.h>#include<uniform.c>intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); DoubleB,c,x;inti,j; Long ints; DoubleUniformDouble,Double,Long int*); b=0.0; c=1.0; s=13579; for(i=0;i<Ten; i++) { for(j=0;j<5; j + +) {x=uniform (b,c,&s); printf ("%13.7f", x); } printf ("\ n"); } returna.exec ();}
Uniform.c
double uniform (double A,double B,longint *seed) {double t; *seed=2045* (*seed) +1; *seed=*seed-(*seed/1048576) *1048576; t= (*seed)/1048576.0; t =a+ (b-a) *t; return (t);}
Second, Gaussian distribution
Double Gauss (double mean,double sigma,long int *s)
Mean positional parameters, Sigma scale parameters, seed random seed
Main.cpp
#include <QCoreApplication>#include<math.h>#include<stdio.h>#include<stdlib.h>#include<gauss.c>intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); inti,j; Long ints; DoubleX,mean,sigma; DoubleGaussDouble,Double,Long int*); Mean=0.0; sigma=1.0; s=13579; for(i=0;i<Ten; i++) { for(j=0;j<5; j + +) {x=gauss (mean,sigma,&s); printf ("%13.7f", x); } printf ("\ n"); } returna.exec ();}
Gauss.c
#include <uniform.c>DoubleGaussDoubleMeanDoubleSigmaLong int*s) { inti; Doublex, y; DoubleUniformDouble,Double,Long int*); for(x=0, i=0;i< A; i++) x+=uniform (0.0,1.0, s); X=x-6.0; Y=mean+x*Sigma; return(y);}
Uniform.c
double uniform (double A,double B,longint *seed) {double t; *seed=2045* (*seed) +1; *seed=*seed-(*seed/1048576) *1048576; t= (*seed)/1048576.0; t =a+ (b-a) *t; return (t);}
Digital signal Processing C language------uniform distribution and Gaussian distribution random number