源代码 import math import random import numpy as np import matplotlib.pylab as plt m = 1000000 n = 0 for i in range(m): x = random.random() y = random.random() if y <= x**3: n += 1 r = n / m print("r = {}".format(r)) x = np.linspace(0,1,num=50) y = x**3 plt.plot(x,y) plt.show()
运行结果