generated from github/welcome-to-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom Walk
61 lines (43 loc) · 901 Bytes
/
Random Walk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* simple random walk simulation */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
FILE *output;
time_t t;
main()
{
int i;
double xrand,yrand;
double x, y, randwalkarr[10001];
output= fopen ("randwalk4.dat", "w"); /* external file name */
for (i=0; i<=10000; i++)
randwalkarr [i]=0.0; /* clear array */
srand((unsigned) time(&t)); /* set the number generator */
x=0.0; y=0.0;
for (i=1;i<=10000; i++)
{
/* generate x random number */
xrand=rand()%1000;
xrand=xrand/1000;
if(xrand<0.5)
x=x+1.0;
else
x=x-1.0;
/* generate y random number */
yrand=rand()%1000;
yrand=yrand/1000;
if(yrand<0.5)
y=y+1.0;
else
y=y-1.0;
randwalkarr[i] = sqrt(x*x+y*y);/* store randwalkarr to total */
}
/* Write values to file */
for (i=0; i<=100; i++)
{
fprintf(output,"%d %lf\n", i, randwalkarr[i*100]);
}
fclose (output);
}