/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * drunken_jogger.c
 * Copyright (C) 2015 drdev <drdev@localhost.com>
 * 
 * drunken_jogger is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * drunken_jogger is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <time.h>
#include <math.h>

#define trials 100000
#define finish 5000.0
#define y_limit 10.0

/* Prototypes */
char *strcpy(char *dest, const char *src);
void exit(int status);
void srand(unsigned int seed);
int rand();
float random_angle();
/* end of prototypes */

char fn1[64];
FILE *outdata;
unsigned long i;
int distance[trials];
float x,y,dx,dy;
float angle;

float random_angle()
{
//returns a random angle in radians from -90 degrees to 90 degrees
return(((rand() % 181)-90)*(1.57079635/90));
}


int main(int argc, char *argv[])
{
if (argc<2)
{
strcpy(fn1,"output.txt");
}
else
{
strcpy(fn1,argv[1]);
}

printf("\nOutput file selected = %s\n",fn1);

if ((outdata = fopen(fn1,"w"))==NULL)
	{printf ("\nOutput file cannot be opened.\n");
	exit (1);}

srand(time(NULL));

for(i=1;i<=trials;i++)
{
distance[i]=0;
x=0;
y=0;

while(x<finish)
{
angle=random_angle();
dx=cos(angle);
dy=sin(angle);
if(((y+dy)<=y_limit)&&((y+dy)>=-y_limit))
{
x = x+dx;
y = y +dy;
distance[i] = distance[i]+1;
//printf("%f\t%f\t%d\n",x,y,distance[i]);
}

}
	
fprintf(outdata,"%ld\t%d\n",i,distance[i]);

}
	
printf("Done\n");

fclose(outdata);

return (0);
}
