from random import randint, random """ RECURSION OR DIRECT COMPUTATION? """ # Recursive implementation of Fibonacci number def fibr(n): if n <= 2: return 1 return fibr(n - 1) + fibr(n - 2) # Prints the list of first n Fibonacci numbers def fibonaccir(n): for i in range(1, n + 1): print(fibr(i), end=" ") print() # Linear implementation of Fibonacci number def fibonacci(n): # Initialization a1 = 1 a2 = 1 # Computes and prints the sequence for i in range(n): print(a1, end=" ") tmp = a2 # Stores the latest value in the sequence a2 = a1 + a2 # Computes new value of the sequence a1 = tmp # Updates the second to last value of the sequence print() # For comparison, both sequences are run to see the time difference print("Linear generation of Fibonacci numbers:") fibonacci(35) print("Recursive generation of Fibonacci numbers:") fibonaccir(35) print() """ RANDOM NUMBERS - SIMPLE EXAMPLES """ # Function simulating roll of a regular dice def dice(): return randint(1, 6) # Rolling three dice print("Throwing three dice:") print(dice(), dice(), dice()) # Function returning def turn(): val = 65536 # Initial 'value' of the dice can be any even number throws = 0 # Counts number of throws while val % 2 == 0: val = dice() # Throwing the dice throws += 1 return throws print("Runs until odd number:") print(turn()) # Printing frequencies of different dice values # For now without the use if lists def dice_freq(count): # a1-a6 will hold the particular counts a1 = 0 a2 = 0 a3 = 0 a4 = 0 a5 = 0 a6 = 0 for i in range(count): d = dice() # dice roll # Incrementation of respective values if d == 1: a1 = a1 + 1 elif d == 2: a2 = a2 + 1 elif d == 3: a3 = a3 + 1 elif d == 4: a4 = a4 + 1 elif d == 5: a5 = a5 + 1 else: a6 = a6 + 1 # Printing the frequencies print("Frequencies of different throws out of",count,"throws:") print("1:", a1, "- 2:", a2, "- 3:", a3, "- 4:", a4, "- 5:", a5, "- 6:", a6) dice_freq(10000) print() """ DRUNKARD'S WALK """ # Function for printing drunkard's status def drunkprint(size, position, printout=True): # If printout is False, we do not print anything and return if not printout: return # If either end has been reached we print information if position == 0: print("Home reached!") elif position == size + 1: print("Ended again in pub!") # If the position is in between, print the position graphically else: print("home", end=" ") for i in range(1, position): print(". ", end="") print("* ", end="") for i in range(position, size): print(". ", end="") print("pub") # Function simulating one run of drunkard # size = distance between home and pub # steps = maximum number of steps # printout = if False printing is omitted def drunkard_simulator(size, steps, printout=True): # Choosing starting position position = size // 2 # One simulation of drunkard while (position >= 1) and (position <= size) and (steps > 0): drunkprint(size, position, printout) steps = steps - 1 position = position + (-1) ** randint(0, 1) # If reached position is 0 (Home) return value is 1 # In other cases we return 0 if position == 0: drunkprint(size, position, printout) return 1 elif position == size + 1: drunkprint(size, position, printout) return 0 print("One simulation of drunkard's walk:") drunkard_simulator(10, 100) # Simulation of 'count' runs of drunkard's walk def drunkman_analysis(size, steps, count): home = 0 # Stores number of home runs for i in range(count): home += drunkman_simulator(size, steps, False) # Returning % of home runs return 100 * home / count print("Simulation of drunkard's walks:") print(drunkman_analysis(10, 100, 1000),"% ended at home...")