Embedded Programming with SQLite and Python Using the tree g
Embedded Programming with SQLite and Python
Using the tree given, starting at the root node (the prompt), you’ll randomly select a child node as described below. The probability that a child gets selected will vary as the simulation runs.
Simulate MAX = 250 times
1st time:
For the 3 child nodes : take path (1,2,or 3) (This is the probability you’ll follow that path)
If take path 1, then add 2 and subtract 1 from the other 2 paths
Do same for paths 2 and 3 *
(* if hit 100 – stop and output the title of the root node)
(* if hit 0 – stop and output the title of that root node)
If hit 250 runs, then output each node and the probabilities
TREE:
Solution
import random
grid = range(4)
trials = 3
moves = [1,0,-1]
numMovesLs=[]
trial=0
while trial < trials:
p1 = [random.choice(grid),random.choice(grid),random.choice(grid),random.choice(grid)]
sameSpot = False if p1[:2] == p1[2:]:
sameSpot = True numMoves=0
while sameSpot == False:
numMoves+=1 posi=0 while posi < len(p1):
if 0 <= p1[posi]+random.choice(moves) <= 99:
p1[posi] = p1[posi]+random.choice(moves)
posi+=1
if p1[:2] == p1[2:]:
sameSpot = True numMovesLs.append(numMoves)
trial+=1
print trial

