Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The classic drunkard\'s walk problem: Over an eight block line, the home of an i

ID: 3680491 • Letter: T

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... 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

Explanation / Answer

The classic drunkard's walk problem

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int N, iTotalMoves, iTotalHome, x, iTotalPub;
float fAverageMoves, fAverageHome, fProbHome;
int B = 1000000;
void Walk(float &fAverageMoves, int &iTotalMoves,
int &iTotalHome, float &fAverageHome, int &N, int &x,
int &iTotalPub, float fProbHome) ;

int Spot;


srand(time(0));


int main()
{
Walk(fAverageMoves, iTotalMoves, iTotalHome, fAverageHome, N, x,
iTotalPub, fProbHome);
return 0;
}

void Walk(float &fAverageMoves, int &iTotalMoves, int &iTotalHome,
float &fAverageHome, int &N, int &x, int &iTotalPub, float fProbHome)
{
fProbHome = rand() % 3;

N=2;

while(2<= N&& 7>=N && N++)
{

if(iTotalHome+iTotalPub <= 1000000)
{N=x;
do
{
if (1< x<8)
{

if(fProbHome == 1 || fProbHome == 2)
{
x=x+1;
}

if(fProbHome == 0)
{
x = x-1;
}
}
}while(x!= 1 && x!= 8);

if(x==1)
{
iTotalHome++;
}
if(x==8)
{
iTotalPub++;
}
iTotalMoves ++;
}
fAverageMoves = iTotalMoves/(N-1);
fAverageHome = iTotalHome/iTotalMoves;
cout << "Average Moves:" << fAverageMoves<< endl;
cout << "Total Home:" << iTotalHome<< endl;
cout << "Total Pub:" << iTotalPub<< endl;
cout << "Total Moves:" << iTotalMoves<< endl;
}
return;
}

OUTPUT

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote