Random Walk Path Generator

  • Share this:

Code introduction


This function starts from a specified start path and performs a random walk with a depth of depth, returning the list of paths during the walk.


Technology Stack : os, random

Code Type : File path traversal

Code Difficulty : Beginner


                
                    
import random
import os
import sys
import re
import time
import datetime
import json

def random_walk(start_path, depth):
    path = [start_path]
    for _ in range(depth):
        new_path = os.path.join(random.choice(path), random.choice(os.listdir(path[-1])))
        path.append(new_path)
    return path                
              
Tags: