main.cpp
所建工程文件如下图所示
uniform.h
#ifndef _UNIFORM_H_ #define _UNIFORM_H_ double uniform(double a,double b, long int *seed); #endif
uniform.cpp
#include "uniform.h" #include "stdio.h"
#define Lamda 2045 #define Miu 1 #define M 1048576
double uniform(double a, double b, long int *seed) { double t; *seed = Lamda*(*seed) + Miu; *seed = *seed - (*seed/M)*M; t = (*seed) / (M+0.0); t = a + (b - a)*t; return(t); }
uniform_test.h
#ifndef _UNIFORM_TEST_H_ #define _UNIFORM_TEST_H_ void uniform_test( void ); #endif |
uniform_test.cpp
#include "uniform_test.h" #include "uniform.h" #include "stdio.h" #include <iostream> #include <iomanip> using namespace std;
void uniform_test(void) { double a =0.0; double b =1.0; double x; long int s=13579; int i,j; printf(" uniform test start! "); for(i=0;i<10;i++) { for(j=0;j<5;j++) { x=uniform(a,b,&s); cout<<setiosflags(ios::fixed)<<setprecision(7); cout << x << ' '; //printf(" %13.7f",x); } cout << ' ' <<endl; //printf(" "); }
}
//#include "uniform.h"#include "uniform_test.h" #include<stdio.h>
void main(void) { uniform_test(); getchar(); }