I'm new here, started learning to code a couple months ago. I have a problem. For phase 2 of this program, but not phase one, the rand() seems to go through the same sequence, because my vectors for the data keeps storing the same values. I've tried using srand(time(0)) and that doesn't seem to help, whether in main, the while loop, or the before it. I've tried mt_rand, but couldn't figure out how to convert an object type to an int type. I think the problem is with rand(), on line 147. But it could be something else? I'm stumped and have been searching for solutions for hours now. Could you please help me? Code below.
#include "stdafx.h"
#include "iostream"
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <ctime>
#include <random>
#include <functional>
using namespace std;
int main();
int loopCounter() {
int loop_count;
cout << "How many repetitions would you like?" << endl;
cin >> loop_count;
cout << endl;
return loop_count;
}
int randomStep() {
int oneOrZero = rand() % 2;
if (oneOrZero == 0) {
oneOrZero = -1;
}
return oneOrZero;
}
string wetnessOutput(int position, string &previousWetness) {
for (int x=0; x < position; x++) {
cout << " ";
}
if (previousWetness == "D" && position > 9 && position < 21) {
cout << 'D' << endl;
previousWetness = "D";
return "D";
}
else {
if (position > 9 && position < 21) {
cout << "WD" << endl;
previousWetness = "WD";
return "WD";
}
else {
cout << "W" << endl;
previousWetness = "W";
return "W";
}
}
}
int phaseOneStep() {
int prevXpos = 15;
string wetness = "D";
const int WORFLENGTH = 50;
for (int steps=0; steps < WORFLENGTH; steps++) {
int Xpos = prevXpos + randomStep();
wetnessOutput(Xpos, wetness);
prevXpos = Xpos;
}
return prevXpos;
}
int phaseOneWalk(int &loopCount, vector <int> &data) {
int totalEndPos = 0;
loopCount = loopCounter();
data.clear();
for (int repititions = 1; repititions <= loopCount; repititions++) {
int endPos = phaseOneStep();
totalEndPos = endPos + totalEndPos;
data.push_back(endPos);
cout << endl << "Repititions completed: " << repititions << endl;
cout << "Press enter for next step." << endl;
cin.ignore();
cin.get();
}
return totalEndPos;
}
int whichPhase(int &phase) {
int phaseChoice;
cout << "Which phase would you like? Type 1 or 2." << endl;
cin >> phaseChoice;
if (1 == phaseChoice || 2 == phaseChoice) {
if (1 == phaseChoice) {
return 1;
}
if (2 == phaseChoice) {
return 2;
}
}
else {
cout << "Invalid input. Use 1 or 2." << endl;
int phase = whichPhase(phase);
return 0;
}
return 0;
}
int findMode(vector<int>data) {
int mode;
int size = data.size();
vector<int>modeGetter(500);
for (int x = 0; x < size; x++) {
int value = data[x];
modeGetter[value] = modeGetter[value]+1;
mode = distance(modeGetter.begin(), max_element(modeGetter.begin(), modeGetter.end()));
}
return mode;
}
void phaseOneCalculations(int totalXpos, vector<int>data) {
double dataSize = data.size();
int mode = 0;
double max = *max_element(data.begin(), data.end());
double min = *min_element(data.begin(), data.end());
double middle = dataSize / 2;
sort(data.begin(), data.end());
mode = findMode(data);
cout << "The mean variance from starting position is: " << fabs((totalXpos / dataSize) - 15) << endl;
cout << "The mean of ending x positions, starting at 15, is: " << totalXpos / dataSize << endl;
cout << "The minimum ending x positions is: " << min << endl;
cout << "The maximum ending x positions is: " << max << endl;
cout << "The median ending x positions is: " << data[middle] << endl;
cout << "The mode of ending x positions is: " << mode << endl;
cout << "Press enter to exit." << endl;
cin.ignore();
cin.get();
exit(1);
}
int getDiameter(double &stepSize) {
int diameter = 0;
cout << "Enter the diameter of the circle. Max 200 Min 20." << endl;
cin >> diameter;
cout << "Enter the size of each step. Max 5 Min 0.1" << endl;
cin >> stepSize;
if (diameter > 19 && diameter < 201 && stepSize >= 0.1 && stepSize <= 5) {
return diameter;
}
else {
cout << endl << "Invalid input, program restarting." << endl;
main();
return 0;
}
}
int phaseTwoSimulate(double &Xpos, double &Ypos, int diameter, double stepSize, int randomHelper) {
int repeateTimes = 0;
while (pow(Xpos - 200, 2) + pow(Ypos - 200, 2) < pow(diameter, 2)) {
int direction = rand() % 8;
repeateTimes = repeateTimes + 1;
switch (direction) {
case 0://north
Ypos = Ypos - stepSize;
break;
case 1://east
Xpos = Xpos + stepSize;
break;
case 2://south
Ypos = Ypos + stepSize;
break;
case 3://west
Xpos = Xpos - stepSize;
break;
case 4://north east
Xpos = Xpos - sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos + sqrt(pow(stepSize, 2) / 2);
break;
case 5://south east
Xpos = Xpos + sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos + sqrt(pow(stepSize, 2) / 2);
break;
case 6://south west
Xpos = Xpos + sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos - sqrt(pow(stepSize, 2) / 2);
break;
case 7://north west
Xpos = Xpos - sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos - sqrt(pow(stepSize, 2) / 2);
break;
default:
cout << "error in randomizer" << endl;
main();
break;
}
}
return repeateTimes;
}
void phaseTwoWalk(int &loopCount, vector<int>&dataX, vector<int>&dataY) {
int totalXpos = 0;
int totalYpos = 0;
loopCount = loopCounter();
int endXpos = 0;
int endYpos = 0;
double Xpos = 201;
double Ypos = 201;
double stepSize = 0;
int diameterCircle = getDiameter(stepSize);
dataX.clear();
dataY.clear();
for (int repititions = 0; repititions < loopCount; repititions++) {
int randomHelp = 0;
repititions = phaseTwoSimulate(Xpos,Ypos,diameterCircle,stepSize,randomHelp);
dataX.push_back(Xpos);
dataY.push_back(Ypos);
randomHelp++;
int Xpos = 201;
int Ypos = 201;
}
}
void phaseTwoCalculations(vector<int>dataX, vector<int>dataY) {
double dataSizeX = dataX.size();
double dataSizeY = dataY.size();
int modeX = 0;
int modeY = 0;
double maxX = *max_element(dataX.begin(), dataX.end());
double minX = *min_element(dataX.begin(), dataX.end());
double maxY = *max_element(dataY.begin(), dataY.end());
double minY = *min_element(dataY.begin(), dataY.end());
double middleX = dataSizeX / 2;
double middleY = dataSizeY / 2;
double sumX = accumulate(dataX.begin(), dataX.end(), 0);
double sumY = accumulate(dataY.begin(), dataY.end(), 0);
sort(dataX.begin(), dataX.end());
sort(dataY.begin(), dataY.end());
modeX = findMode(dataX);
modeY = findMode(dataY);
cout << "The center of the circle is 201,201" << endl;
cout << "The mean variances from starting position are (x,y): " << (fabs((sumX / dataSizeX)) - 201) <<
"," << (fabs((sumY / dataSizeY) - 201)) << endl;
cout << "The mean of ending x,y positions are: " << sumX / dataSizeX << "," <<
sumY / dataSizeY << endl;
cout << "The minimum ending x,y positions are: " << minX << "," << minY << endl;
cout << "The maximum ending x,y positions are: " << maxX << "," << maxY << endl;
cout << "The medians ending x,y positions are: " << dataX[middleX] << "," << dataY[middleY] << endl;
cout << "The mode of ending x,y positions are: " << modeX << "," << modeY << endl;
//add the number of steps average, repititions.
cout << "Press enter to exit." << endl;
cin.ignore();
cin.get();
exit(1);
}
int main()
{
int repititions;
vector<int>phaseOneData;
vector<int>phaseTwoDataX;
vector<int>phaseTwoDataY;
int phase = whichPhase(phase);
if (phase == 1) {
int totalPos = phaseOneWalk(repititions, phaseOneData);
phaseOneCalculations(totalPos, phaseOneData);
}
if (phase == 2) {
phaseTwoWalk(repititions, phaseTwoDataX, phaseTwoDataY);
phaseTwoCalculations(phaseTwoDataX, phaseTwoDataY);
}
if (phase == 0) {
cout << endl << "Phase error, program restarting." << endl;
main();
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire