!pip install matplotlib numpy
import numpy as np
import matplotlib.pyplot as plt
#---------------------------------------------
# パラメータ設定 / Parameters
#---------------------------------------------
N = 5000 # サンプル数(投擲回数)
radius = 1.0 # 半径 [m]
#---------------------------------------------
# モンテカルロ法による円面積推定 / Monte Carlo Estimation
#---------------------------------------------
x = np.random.uniform(-radius, radius, N)
y = np.random.uniform(-radius, radius, N)
inside = x**2 + y**2 <= radius**2
pi_estimate = 4 * np.sum(inside) / N
#---------------------------------------------
# モンスターボール描画 / Poké Ball Visualization
#---------------------------------------------
fig, ax = plt.subplots(figsize=(6,6))
# 上半分(赤) / Upper half (red)
theta = np.linspace(0, np.pi, 200)
ax.fill_between(np.cos(theta)*radius, 0, np.sin(theta)*radius, color='red', alpha=0.9)
# 下半分(白) / Lower half (white)
ax.fill_between(np.cos(theta)*radius, 0, -np.sin(theta)*radius, color='white', alpha=1.0)
# 中央ライン(黒) / Center line
ax.plot([-radius, radius], [0, 0], color='black', linewidth=3)
# 中央ボタン / Center button
button_outer = plt.Circle((0,0), 0.25, color='black', fill=True)
button_inner = plt.Circle((0,0), 0.15, color='white', fill=True)
ax.add_patch(button_outer)
ax.add_patch(button_inner)
# モンテカルロ点(ポケモン捕獲シミュレーション) / Scatter points
ax.scatter(x[inside], y[inside], color='blue', s=6, label='Captured (Inside)')
ax.scatter(x[~inside], y[~inside], color='gray', s=6, label='Missed (Outside)')
#---------------------------------------------
# 表示設定 / Display settings
#---------------------------------------------
ax.set_title(f"Poké Ball Monte Carlo Simulation (π ≈ {pi_estimate:.4f})", fontsize=13)
ax.set_xlabel("Horizontal distance [m]")
ax.set_ylabel("Vertical distance [m]")
ax.set_aspect('equal')
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-1.1, 1.1)
ax.legend()
plt.show()
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
