Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <time.h>
- typedef unsigned long long int RANDOM_TYPE;
- RANDOM_TYPE a, b, m, seed, randomNumber;
- struct{
- double x,y,z;
- }p;
- double distanza(double x1, double x2, double x3);
- double distribuzione(double fstoredistanzarandomnumber[], int fpassi, int fn_bin);
- int main(){
- double r, xt2, arraybin[10], distanzatot, media, bin=10000;
- int position, i, c,g,passi=100,n_bin=70;
- double storedistanzarandomnumber[passi];
- i=0; media=0;
- p.x=0; p.y=0; p.z=0;
- position=0;
- i=0;
- a=16807;
- m=2147483647;
- seed=222;
- randomNumber=seed;
- FILE *fp;
- fp=fopen("casuale_3D.dat","w+");
- for(i=1; i<passi; i++){
- fprintf(fp,"%d %lf %lf %lf %Lu \n", i,p.x,p.y,p.z,randomNumber);
- randomNumber=(a*randomNumber)%m;
- r=(double)randomNumber/(double)m;
- if(r<(1/6.)){
- p.z+=1;
- }
- else if (r<(2/6.)){
- p.z-=1;
- }
- else if (r<(3/6.)){
- p.x+=1;
- }
- else if (r<(4/6.)){
- p.x-=1;
- }
- else if (r<(5/6.)){
- p.y+=1;
- }
- else if (r<1){
- p.y-=1;
- }
- storedistanzarandomnumber[i]=distanza(p.x,p.y,p.z);
- }
- FILE *ff;
- ff=fopen("casuale_distanza.dat","w+");
- for(i=1; i<passi; i++){
- fprintf(ff,"%lf \n",*(storedistanzarandomnumber+i));
- }
- distribuzione(storedistanzarandomnumber, passi, n_bin);
- }
- double distanza(double x1,double x2,double x3){
- double dist;
- dist=sqrt(x1*x1+x2*x2+x3*x3);
- return dist;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- double distribuzione(double fstoredistanzarandomnumber[], int fpassi, int fn_bin){
- FILE *fq;
- int i, determinobin;
- double max, arraybin[fn_bin], grandezzabin;
- //determino distanza max/////////
- for(i=1; i<fpassi; i++){
- if(max<fstoredistanzarandomnumber[i]){max=fstoredistanzarandomnumber[i];}
- }
- printf("il massimo รจ %lf",max);
- grandezzabin=max/fn_bin;//determino grandezza bin
- //riempio bin/////////
- i=0;
- for(i=1; i<fpassi; i++){
- determinobin=fstoredistanzarandomnumber[i]/grandezzabin;
- printf(" %d*bin = %d \n",i,determinobin);
- arraybin[determinobin]+=1;
- }
- //scrivo distribuzione///////////
- fq=fopen("casuale_distr_dista.dat","w+");
- i=0;
- for(i=1; i<fpassi; i++){
- if(arraybin[i]!=0){fprintf(fq,"%d %lf \n",i,arraybin[i]);}
- }
- system("echo 'plot \"casuale_distr_dista.dat\" u 1:2 with lines'|gnuplot -persist");
- system("echo 'splot \"casuale_3D.dat\" u 2:3:4 with lines'|gnuplot -persist");
- }
Advertisement
Add Comment
Please, Sign In to add comment