def RandomPrecision(k,l): m = Random(2) bf = Random(pow(10,k))-1 af = Random(pow(10,l))-1 af = af / pow(10,l) x = bf+af if m == 1: x = x*(-1) return x
def which_way_to_turn(self): # create a random number generator rand = Random() if self.intersectionID == predefines.INTERSECTION1: # can't be coming down if self.direction == predefines.UP: # can go down, left, or right self.path = rand.og_randint(1, 3) elif self.direction == predefines.LEFT: # down, left or up self.path = rand.og_randint(0, 2) elif self.direction == predefines.RIGHT: # up, down, or right self.path = rand.skip_randint(0, 3, 2) else: return elif self.intersectionID == predefines.INTERSECTION2: # can't be coming right if self.direction == predefines.UP: # can go up, down, or right self.path = rand.skip_randint(0, 3, 2) elif self.direction == predefines.DOWN: # up, down, left self.path = rand.og_randint(0, 2) elif self.direction == predefines.LEFT: # down, left, right self.path = rand.og_randint(1, 3) else: return elif self.intersectionID == predefines.INTERSECTION3: # can go anywhere self.path = rand.og_randint(0, 3) # up, down, left, right elif self.intersectionID == predefines.INTERSECTION4: # can't be coming left if self.direction == predefines.UP: # can go up, down, or left self.path = rand.og_randint(0, 2) elif self.direction == predefines.DOWN: # up, down, right self.path = rand.skip_randint(0, 3, 2) elif self.direction == predefines.RIGHT: # down, left, right self.path = rand.og_randint(1, 3) else: return elif self.intersectionID == predefines.INTERSECTION5: # can't be coming up if self.direction == predefines.RIGHT: # can go up, down, or right self.path = rand.og_randint(0, 2) elif self.direction == predefines.DOWN: # down, left, right (can't go up) self.path = rand.og_randint(1, 3) elif self.direction == predefines.LEFT: # up, down, left self.path = rand.skip_randint(0, 3, 2) else: return else: return
def RandomInterval(k,l): x = Random(l-k+1)+k-1 return x
def main(): """Starts the program""" # check command lines argument count if len(sys.argv) < 3: print("Not enough arguments:\n\tUsage: ./test.py path/to/board" + " random/breadth/depth [amount of tries]") sys.exit(1) # defines how many times a solution is searched for if len(sys.argv) == 4: tries = int(sys.argv[3]) else: tries = 1 # defines the path to the board that needs solving path = str(sys.argv[1]) # counts how many solutions have been found already count = 0 # get start time for basic benchmarking t0 = time.time() # solve using random search if sys.argv[2] == "random": while count < tries: # get current system time time0 = time.time() # sets up a start board from file board = setup_board(path) # creates a random instance random = Random(board) # starts the random algorithm to solve the puzzle random.solve() # count how many solutions have been found count += 1 # solve using breadth first elif sys.argv[2] == "breadth": board = setup_board(path) breadth = Breadth(board) breadth.solve() # solve using depth first elif sys.argv[2] == "depth": board = setup_board(path) depth = Depth(board) depth.solve() # invalid commandline arguments else: print("Usage: ./test.py path/to/board random/breadth/depth" + "[amount of tries]") sys.exit(2) # print benchmark time t1 = time.time() print("Total time: {}".format(t1 - t0))