Revision: 14189
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 22, 2009 10:10 by mustam
Initial Code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define R (M_PI*4)
#define r (M_PI*2)
int main(int argc, char **argv)
{
int i, step;
double x=R+r, y=0.0, z=0.0;
double theta=0.0, phi=0.0, omega;
if(argc!=2 || (step=atoi(argv[1]))<=0){
fprintf(stderr, "Usage: %s <step-number>\n", argv[0]);
exit(EXIT_FAILURE);
}
srand((unsigned)time(NULL));
printf("%.3f\t%.3f\t%.3f\n", x, y, z);
for(i=0; i<step; i++){
omega=(double)rand() / (double)RAND_MAX * M_PI*2;
theta+=cos(omega)/R;
if(theta<0) theta+=M_PI*2;
else if(theta>M_PI*2) theta-=M_PI*2;
phi+=sin(omega)/r;
if(phi<0) phi+=M_PI*2;
else if(phi>M_PI*2) phi-=M_PI*2;
x=(R+r*cos(phi))*cos(theta);
y=(R+r*cos(phi))*sin(theta);
z=r*sin(phi);
printf("%.3f\t%.3f\t%.3f\n", x, y, z);
}
return 0;
}
Initial URL
Initial Description
% gcc -lm rndwalk-torus.c<br> % ./a.out 1000 > rnd1.data<br> % gnuplot<br> gnuplot> splot "rnd1.data" with line
Initial Title
Random walking on a torus
Initial Tags
Initial Language
C