Generate random path in grid

I’m trying to generate a random path in a 3x3 grid and don’t know where to start. What I want to do is have it always start in the bottom center room and end in the top center room, and set values in an array to a different number depending on where there needs to be doors depending on the path. (eg. top-left room (number 0 in array) gets set to 3 because it needs to have a door on the south wall and the east wall. another function then reads those variables to know what kind of room it needs to build) Currently I have made paths that it will choose from but obviously this can get figured out easily. How would I go about doing this so that no rooms are left out?

Since you already fixed the start and end points for your path that makes things lot less simple.

you can do something like the following:

  1. chose a start point. let this be your current position.
  2. get a list of all reachable positions from the current position in all valid directions. (these positions should be the grid-cells adjacent to the current position)
  3. select a random position from the list
  4. move to the new position
  5. if the new position is the end position, return
  6. goto 2

you might end up with longer paths sometime because you are choosing your next positions randomly. if you want to be sure that your path should not get longer beyond a certain length, you can put some logic in choosing your next position from the list of reachable positions. For eg; select a random position from the list whose distance from the end point is within a certain range.