Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:40:15+00:00 2026-06-17T17:40:15+00:00

I am working on a simple Monte-Carlo simulation script, which I will later on

  • 0

I am working on a simple Monte-Carlo simulation script, which I will later on extend for a larger project. The script is a basic crawler trying to get from point A to point B in a grid. The coordinates of point A is (1,1) (this is top left corner), and the coordinates of point B is (n,n) (this is bottom right corner, n is the size of the grid).

Once the crawler starts moving, there are four options, it can go left, right, up or down (no diagonal movement allowed). If any of these four options satisfy the following:

  1. The new point should still be within the boundries of the n x n grid
  2. The new point should not be visited previously

the new point will be selected randomly among the remaining valid options (as far as I know Python uses the Mersenne Twister algorithm for picking random numbers).

I would like to run the simulation for 1,000,000 times (the code below runs for 100 only), and each iteration should be terminated either:

  1. The crawler gets stuck (no valid options for movement)
  2. The crawler gets to the final destination (n,n) on the grid.

I thought I implemented the algorithm correctly, but obviously something is wrong. No matter how many times I run the simulations (100 or 1,000,000), I only get 1 successful event wehere the crawler manages to get to the end, and rest of the attempts (99, or 999,999) is unsuccessful.

I bet there is something simple I am missing out, but cannot see it for some reason. Any ideas?

Thanks a bunch!

EDIT: Some typos in the text were corrected.

import random

i = 1 # initial coordinate top left corner
j = 1 # initial coordinate top left corner
k = 0 # counter for number of simulations
n = 3 # Grid size

foundRoute = 0 # counter for number of cases where the final point is reached
gotStuck = 0 # counter for number of cases where no valid options found
coordList = [[i, j]]

while k < 100:
    while True:
        validOptions = []

        opt1 = [i - 1, j]
        opt2 = [i, j + 1]
        opt3 = [i + 1, j]
        opt4 = [i, j - 1]

        # Check 4 possible options out of bound and re-visited coordinates are
        # discarded:

        if opt1[0] != 0 and opt1[0] <= n and opt1[1] != 0 and opt1[1] <= n:
            if not opt1 in coordList:
                validOptions.append(opt1)

        if opt2[0] != 0 and opt2[0] <= n and opt2[1] != 0 and opt2[1] <= n:
            if not opt2 in coordList:
                validOptions.append(opt2)

        if opt3[0] != 0 and opt3[0] <= n and opt3[1] != 0 and opt3[1] <= n:
            if not opt3 in coordList:
                validOptions.append(opt3)

        if opt4[0] != 0 and opt4[0] <= n and opt4[1] != 0 and opt4[1] <= n:
            if not opt4 in coordList:
                validOptions.append(opt4)

        # Break loop if there are no valid options
        if len(validOptions) == 0:
            gotStuck = gotStuck + 1
            break

        # Get random coordinate among current valid options
        newCoord = random.choice(validOptions)

        # Append new coordinate to the list of grid points visited (to be used
        # for checks)
        coordList.append(newCoord)

        # Break loop if lower right corner of the grid is reached
        if newCoord == [n, n]:
            foundRoute = foundRoute + 1
            break

        # If the loop is not broken, assign new coordinates
        i = newCoord[0]
        j = newCoord[1]
    k = k + 1

print 'Route found %i times' % foundRoute
print 'Route not found %i times' % gotStuck
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:40:16+00:00Added an answer on June 17, 2026 at 5:40 pm

    Your problem is that you’re never clearing out your visited locations. Change your block that breaks out of the the inner while loop to look something like this:

     if len(validOptions) == 0:
         gotStuck = gotStuck + 1
         coordList = [[1,1]]
         i,j = (1,1)
         break
    

    You’ll also need to change your block where you succeed:

    if newCoord == [n, n]:
        foundRoute = foundRoute + 1
        coordList = [[1,1]]
        i,j = (1,1)
        break
    

    Alternatively, you could simply place this code right before your inner while loop. The start of your code would then look like:

    k = 0 # counter for number of simulations
    n = 3 # Grid size  
    foundRoute = 0 # counter for number of cases where the final point is reached
    gotStuck = 0 # counter for number of cases where no valid options found
    while k < 100:
        i,j = (1,1) 
        coordList = [[i,j]]
        while True:
            #Everything else
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working to create a simple Python script that will ultimately tell you how
I'm trying to make a simple text editor with winapi, its working for simple
im working on a simple C++ HTTP server as a school project and I
While working on a personal project I wanted a simple service to extract items
I am working on a simple point of sale system and was trying to
I am working on a simple project involving Prospects and Offers . This project
I'm currently working on a program that will calculate a simple interest rate and
The simple working query, list the number of licenses for each license type: Query:
Good Day, I have a simple working routine in Perl that swaps two words:
Working on a simple C program I'm stuck with an if test: int line_number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.