current position:Home>Python simulation random coin toss (non optimized version)

Python simulation random coin toss (non optimized version)

2021-08-22 05:22:49 The clouds are deep and boundless

import random
import numpy as np
import matplotlib.pyplot as plt

# 0->Heads
# 1->Tails

def coin_flip():
    """ The value that appears is 0 perhaps 1"""
    return random.randint(0, 1)

#  Coin function 
#  A function used for recording 
list1 = []
# list Is a built-in function 

def monte_carlo(n):
    result = 0
    for i in range(n):
        flip_result = coin_flip()
        result = result + flip_result

        prob_value = result / (i+1)


        plt.axhline(y=0.5, color='r', linestyle='-')
    return result/n

answer = monte_carlo(10)
print(" Final result :", answer)

10 Results of

100 Time

500 Time

1000 Time

5000 Results and 1000 The results are similar , This requires more computational power

This article is from WeChat official account. - The cloud is deep and traceless (TT1827652464)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2021-08-04

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

copyright notice
author[The clouds are deep and boundless],Please bring the original link to reprint, thank you.