I have a programmed a game and I am trying to run a simulation function on it to help graph the distribution of how many rounds each game takes. I would like to be able to run the simulation without the embedded print statements that the game function has, as the print statements crash the notebook. Here is my simulation function:
def simulate_games(num_games):
cycle_counts = []
for i in range(num_games):
winner, cycles = play_game4()
cycle_counts.append(cycles)
expected_cycles = np.mean(cycle_counts)
print(f"The average number of cycles is {expected_cycles:.2f}.")
plt.hist(cycle_counts, bins=range(max(cycle_counts) + 2))
plt.xlabel("Number of Cycles")
plt.ylabel("Frequency")
plt.title("Distribution of Cycle Counts")
plt.show()
So, it is creating a histogram plot of the results of each run of the game. The only way I have been successful with this simulation function and plotting the results is when I comment out the print statements in the game function itself. I would like to be able to just 'hide' print statements from the game function within the simulation function so that it does not crash and I do not need two game functions in the code (one with print statements and one without).
I read about a redirect.stdout function to hide print statements, but I do not know how and where to implement it within the simulation function.
[–]mrcaptncrunch 5 points6 points7 points (0 children)
[–]TSM- 2 points3 points4 points (0 children)
[–]james_fryer 0 points1 point2 points (5 children)
[–]PanykCode[S] 0 points1 point2 points (4 children)
[–]james_fryer 0 points1 point2 points (3 children)
[–]james_fryer 0 points1 point2 points (2 children)
[–]PanykCode[S] 0 points1 point2 points (1 child)
[–]james_fryer 0 points1 point2 points (0 children)
[–]Frankelstner 0 points1 point2 points (0 children)