-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandwalk.cpp
45 lines (44 loc) · 1.06 KB
/
randwalk.cpp
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
// randwalk.cpp -- using the VEctor class
// compile with the vect.cpp file
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vect.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0)); // seed random-number generator
double direction;
Vector step;
Vector result (0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
cout << "Enter target distance (q to quit): "; // input 50
while (cin >> target)
{
cout << "Enter step length: "; // input 2
if (! (cin >> dstep)) {
break;
}
while (result.magval() < target) {
direction = rand() % 360;
step.set (dstep, direction, 'p');
result = result + step;
steps++;
}
cout << "After " << steps << " steps, the subject "
"has the following location: \n";
cout << result << endl;
result.polar_mode();
cout << " or\n" << result << endl;
cout << "Average outward distance per step = "
<< result.magval() / steps << endl;
steps = 0;
result.set(0.0, 0.0);
cout << "Enter target distance (q to quit)";
}
cout << "Bye!\n";
return 0;
}