www.pudn.com > writenoise_cpp.rar > writenoise.cpp


#ifndef _RANDOM_H 
#define _RANDOM_H 
 
int seed; 
void setSeed(int s) 
{ 
	seed=s; 
} 
 
/* L'Eculer with Bays-Durham shuffle and added safeguards. 
Return a uniform random deviate between 0.0 and 1.0(exclusive of the endpoint values). 
Call with seed a negative integer to initialize; thereafter, do not alter seed 
between successive deviates in a sequence. RNMX should approaimate the largest floating 
value that is less than 1. 
this algorithm is ok when the required random numbers large than 100,000,000.*/ 
 
double ranLE(int &seed) 
{ 
	const int IM1=2147483563, IM2=2147483399; 
	const int IA1=40014,IA2=40692,IQ1=53668,IQ2=52774; 
	const int IR1=12211,IR2=3791,NTAB=32,IMM1=IM1-1; 
	const int NDIV=1+IMM1/NTAB; 
	const double EPS=3.0e-16,RNMX=1.0-EPS,AM=1.0/double(IM1); 
	static int seed2=123456789,iy=0; 
	static int iv[NTAB]; 
	int j,k; 
	double temp; 
	 
	if(seed<0) //initialize 
	{ 
	   seed=(seed==0 ? 1 : -seed); // Be sure to prevent seed=0. 
	   seed2=seed; 
	   for(j=NTAB+7;j>=0;j--) // load the shuffle table(after 8 warm-ups) 
	   { 
	     k=seed/IQ1; 
	     seed=IA1*(seed-k*IQ1)-k*IR1; 
	     if(seed<0) 
	      seed+=IM1; 
	     if(jRNMX) // Because users don't expect endpoint values. 
	   return RNMX; 
	else  
	   return temp; 
} 
 
/* Park and Miller with Bays-Durham shuffle and added safeguards. Returns a uniform  
* random deviate between 0.0 and 1.0(exclusive of the endpoint  
* values). Call with seed a negative integer to initialize; thereafter, do not alter seed 
between successive deviates in a sequence. RNMX should approaimate the largest floating 
value that is less than 1. 
this algorithm is ok when the required random numbers less than  
* 100,000,000. */ 
double ranPM(int &seed) 
{ 
	const int IA=16807,IM=2147483647,IQ=127773,IR=2836,NTAB=32; 
	const int NDIV=(1+(IM-1)/NTA; 
	const double EPS=3.0e-16,AM=1.0/IM,RNMX=(1.0-EPS); 
	static int iy=0; 
	static int iv[NTAB]; 
	int j,k; 
	double temp; 
	 
	if(seed<=0 || !iy) // initialize, be sure to prevent seed=0. 
	{ 
	   if(-seed<1)  
	     seed=1; 
	   else  
	     seed=-seed; 
	   for(j=NTAB+7;j>=0;j--) 
	   { 
	     k=seed/IQ; 
	     seed=IA*(seed-k*IQ)-IR*k; 
	     if(seed < 0) 
	      seed += IM; 
	     if(jRNMX) // because users don't expect endpoint values. 
	   return RNMX; 
	else  
	   return temp; 
 
} 
 
double rnorm() 
{ 
	static int iset=0; 
	static double gset; 
	double fac,rsq,v1,v2; 
	 
	if(seed<0) // reinitialize 
	   iset=0; 
	cout <=1.0||rsq==0.0);// they are not, try again. 
	   fac=sqrt(-2.0*log(rsq)/rsq); 
	// Now make the Box-Muller transformation to get   
	// two normal deviates. Return one and save the other for next time. 
	   gset=v1*fac; // we have an extra deviate handy,  
	   iset=1; // so unset the flat,  
	   return v2*fac; // and return it.  
	} 
	else  
	{ 
	   iset=0; 
	   return gset; 
	} 
} 
 
#endif /* _RANDOM_H */ 
 
#include  
#include "Random.h" 
using namespace std; 
 
int main() 
{ 
	setSeed(-5); 
	for (int i=0;i<100;i++) 
	{  
	cout<< rnorm()<