------------------------------------------------------------------------- The cl
ID: 3861408 • Letter: #
Question
-------------------------------------------------------------------------The classic drunkard's walk problem: Over an eight block line, the home of an intoxicated chap is at block 8, and a pub is at block 1. Our poor friend starts at block n, 2 <= n <= 7, and wanders at random, one block at a time, either toward or away from home. At any intersection, he moves toward the pub with a certain probability, say 2/3, and towards home with a certain probability, say 1/3. Having gotten either home or to the pub, he remains there. Write a program to simulate 1000000 trips in which he starts at block 2, another 1000000 in which he starts at block 3, and so forth up through block 7. For each starting point, calculate and print the proportion of the time he ends up at home and the average number of blocks he walked on each trip.
For the random number generation...
Use the 'srand' function to seed the random number generator. Use the 'rand' function (and mod as needed) to get random probabilities for a given step.
************************************************************************ * * * To do each single walk, call a function that runs through a loop * * until the walk is done. * * * ************************************************************************
-------------------------------------------------------------------------
A sample run might look something like...
lab6
I = 2 TotalMoves = 2834110 Avg moves = 2.834110 total Home = 7820 avg home = 0.0078200
I = 3 TotalMoves = 5503639 Avg moves = 5.503639 total Home = 23621 avg home = 0.0236210
I = 4 TotalMoves = 7830959 Avg moves = 7.830959 total Home = 54851 avg home = 0.0548510
I = 5 TotalMoves = 9519658 Avg moves = 9.519658 total Home = 118166 avg home = 0.1181660
I = 6 TotalMoves = 9875095 Avg moves = 9.875095 total Home = 243413 avg home = 0.2434130
I = 7 TotalMoves = 7581532 Avg moves = 7.581532 total Home = 496420 avg home = 0.4964200
------------------------------------------------------------------------- Please write the program in computer and test it, and copy the program and poste it in the answer Thank you -------------------------------------------------------------------------
The classic drunkard's walk problem: Over an eight block line, the home of an intoxicated chap is at block 8, and a pub is at block 1. Our poor friend starts at block n, 2 <= n <= 7, and wanders at random, one block at a time, either toward or away from home. At any intersection, he moves toward the pub with a certain probability, say 2/3, and towards home with a certain probability, say 1/3. Having gotten either home or to the pub, he remains there. Write a program to simulate 1000000 trips in which he starts at block 2, another 1000000 in which he starts at block 3, and so forth up through block 7. For each starting point, calculate and print the proportion of the time he ends up at home and the average number of blocks he walked on each trip.
For the random number generation...
Use the 'srand' function to seed the random number generator. Use the 'rand' function (and mod as needed) to get random probabilities for a given step.
************************************************************************ * * * To do each single walk, call a function that runs through a loop * * until the walk is done. * * * ************************************************************************
-------------------------------------------------------------------------
A sample run might look something like...
lab6
I = 2 TotalMoves = 2834110 Avg moves = 2.834110 total Home = 7820 avg home = 0.0078200
I = 3 TotalMoves = 5503639 Avg moves = 5.503639 total Home = 23621 avg home = 0.0236210
I = 4 TotalMoves = 7830959 Avg moves = 7.830959 total Home = 54851 avg home = 0.0548510
I = 5 TotalMoves = 9519658 Avg moves = 9.519658 total Home = 118166 avg home = 0.1181660
I = 6 TotalMoves = 9875095 Avg moves = 9.875095 total Home = 243413 avg home = 0.2434130
I = 7 TotalMoves = 7581532 Avg moves = 7.581532 total Home = 496420 avg home = 0.4964200
------------------------------------------------------------------------- Please write the program in computer and test it, and copy the program and poste it in the answer Thank you -------------------------------------------------------------------------
The classic drunkard's walk problem: Over an eight block line, the home of an intoxicated chap is at block 8, and a pub is at block 1. Our poor friend starts at block n, 2 <= n <= 7, and wanders at random, one block at a time, either toward or away from home. At any intersection, he moves toward the pub with a certain probability, say 2/3, and towards home with a certain probability, say 1/3. Having gotten either home or to the pub, he remains there. Write a program to simulate 1000000 trips in which he starts at block 2, another 1000000 in which he starts at block 3, and so forth up through block 7. For each starting point, calculate and print the proportion of the time he ends up at home and the average number of blocks he walked on each trip.
For the random number generation...
Use the 'srand' function to seed the random number generator. Use the 'rand' function (and mod as needed) to get random probabilities for a given step.
************************************************************************ * * * To do each single walk, call a function that runs through a loop * * until the walk is done. * * * ************************************************************************
-------------------------------------------------------------------------
A sample run might look something like...
lab6
I = 2 TotalMoves = 2834110 Avg moves = 2.834110 total Home = 7820 avg home = 0.0078200
I = 3 TotalMoves = 5503639 Avg moves = 5.503639 total Home = 23621 avg home = 0.0236210
I = 4 TotalMoves = 7830959 Avg moves = 7.830959 total Home = 54851 avg home = 0.0548510
I = 5 TotalMoves = 9519658 Avg moves = 9.519658 total Home = 118166 avg home = 0.1181660
I = 6 TotalMoves = 9875095 Avg moves = 9.875095 total Home = 243413 avg home = 0.2434130
I = 7 TotalMoves = 7581532 Avg moves = 7.581532 total Home = 496420 avg home = 0.4964200
------------------------------------------------------------------------- Please write the program in computer and test it, and copy the program and poste it in the answer Thank you
Explanation / Answer
I have not tested it completely due to some technical issues but it should work:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int N, totalMoves;
float averageMoves, averageHome;
int totalHome=0;
int totalPub=0;
int x, probHome;
int Prob(int &probHome);
int StartPos(int &N, int &x, void Walk(int x, int probHome, int &totalMoves, int &totalHome, int &totalPub, int Prob(int &probHome), void Print(float &averageMoves, float &fAveragHome, int totalMoves, int totalHome)));
void Walk(int probHome, int x, int &totalMoves, int &totalHome, int &totalPub, float &averageMoves, float &averageHome, int Prob(int &probHome),
void Print(float &averageMoves, float &averageHome, int totalMoves, int totalHome));
void Print(float &averageMoves, float &averageHome, int totalMoves, int totalHome);
srand(time(0));
StartPos(N, x, Walk(N, probHome, x, totalMoves, totalHome, totalPub, averageMoves, averageHome,
Prob(probHome)));
return 0;
}
int StartPos(int &N, int &x, void Walk(int x, int probHome, int &totalMoves, int &totalHome,
int &totalPub, int Prob(int &probHome), void Print(float &averageMoves, float &averageHome,
int totalMoves, int totalHome)))
{
cin >> N;
x=N;
if(2<=N&& N<=7)
{
Walk(x, probHome, totalMoves, totalHome, totalPub, averageMoves, averageHome, Prob(probHome),
Print(averageMoves, averageHome, totalMoves, totalHome) );
}
return N;
}
int Prob(int &probHome)
{
probHome = rand()%6;
cout<< probHome<< endl;
return probHome;
}
void Walk(int probHome, int x, int &totalMoves, int &totalHome, int &totalPub,
int Prob(int &probHome), void Print(float &averageMoves, float &averageHome,
int totalMoves, int totalHome))
{
int T;
T=1000000;
do
{
Prob(probHome);
if(probHome == 1 || probHome == 2 || probHome == 3 || probHome ==5)
{
x=x+1;
totalMoves++;
}
if(probHome == 0 || probHome == 4)
{
x=x-1;
totalMoves++;
}
if(x==1)
{
totalHome++;
}
if(x==8)
{
totalPub++;
}
T--;
}while(T>= 0);
Print(averageMoves, averageHome, totalMoves, totalHome);
}
void Print(float &averageMoves, float &averageHome, int totalMoves, int totalHome)
{
averageMoves= totalMoves/1000000;
averageHome = totalHome/1000000;
cout << "I = " << N << endl;
cout << "Average Moves = " << averageMoves << endl;
cout << "Total Moves = " << totalMoves << endl;
cout << "AverageHome = " << averageHome << endl;
cout << "TotalHome = " << totalHome << endl;
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.