All pastes #188655 Raw Edit

Stuff

public cpp v1 · immutable
·published 2006-10-02 10:55 UTC
rendered paste body
//Paragwgh tyxaiwn arithmwn kanonikhs katanomhs me Monte-Carlo methodo.//To aoristo oloklhrwma ths f(x):Gauss den yparxei kai efarmozw monte-carlo.#include <cstdlib>#include <cmath>#include <time.h>#include <iostream>#include <fstream>#include <string>using namespace std;int main(){   	const int a=0,b=1000;	double x,y,f,m,s;	int i=0,j;	cout<<"\n";	cout<<" PARGWGH TYXAIWN ARITHMWN KANONIKHS KATANOMHS "<<"\n\n";//Mporeis na baleis pollapla \n	cout<<"\tDwse thesh koryfhs:"<<"\n";//To \t afhnei proka8orismeno keno	cin>>m;	cout<<"\tDwse diaspora:"<<"\n";	cin>>s;	cout<<"\tPlhthos tyxaiwn arithmwn:"<<"\n";	cin>>j;	cout<<"\n"; 	time_t seconds; 	time(&seconds); 	srand((unsigned int) seconds);	//Proetoimasia arxeiou e3odou. Ta dedomena 8a tupw8oune se ena arxeio "data.csv"	//mesa ston fakelo pou uparxei to .exe sou.	string outputname = "data.csv";	ofstream file;	if (outputname != "")	{		if (outputname.rfind(".csv") == -1)			outputname += ".csv";		file.open(outputname.c_str());		if (!file)		{			cerr << "Sfalma: De mporw na anoi3w auto to arxeio (" << outputname << ")." << endl;			exit(-1);		}	}	char* buffer = 0;	buffer = new char[2000];	//Telos proetoimasia arxeiou e3odou	while(i<j)	{		//tha paraksw tyxaious omoiomorfhs katanomhs sto (m-4s,m+4s) 		x=(float)(rand()%(b-a)+a)/1000.0;//x :tyxaio omoiomorfhs katanomhs sto (0,1)		x=(x-0.5)*8*s+m;//to (0,1) metaferetai sto (-0.5,0.5) kai diasteletai kata 		// 8 diaspores ,meta metaferetai gyro apo thn mesh timh m.		//Grafw thn f(x):Gauss synarthsei twn m,s  		f=(pow(2.718281828,-(pow((x-m)/s,2))/2)) / (s*(sqrt(2*3.141592654)));		//Zari... y tyxaio omoiomorfhs katanomhs sto (o , f max)		y=(float)(rand()%(b-a)+a)/1000.0;  //y sto (0,1)		y=y/(s*(sqrt(2*3.1415926)));       //y sto (0 , f max)		if(f>=y) 		{			cout<<" "<<x<<"\n";i++;			sprintf(buffer, "%f", x);			char* p = buffer; while(*p) {if (*p == '.') *p = ','; p++;}			if (file) file << buffer << endl;		}   	}	//Kleinei to arxeio	delete[] buffer;	if (file)		file.close();	//arxeio papala	return 0;}