0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ポケモンの「だいもんじ」は本当に当たる?精度の高いRNGとBox-Muller法で命中率85%をシミュレート

Posted at

📌 概要

ポケモンに登場する強力な炎技「だいもんじ(命中率85%)」の当たる・外れる挙動を、Pythonで数値シミュレーションしてみました。
本稿では2つの乱数生成法を比較します:

  1. 高精度な一様乱数生成器 default_rng()
  2. 教科書で有名な Box-Muller 法(正規乱数を変換)

どちらも**「10000回だいもんじを撃ったらどうなるか?」**を検証します。


⚙️ シミュレーション条件

  • 命中率:85%
  • 試行回数:10,000回
  • 命中の判定:一様乱数が0.85未満 → 命中
  • Box-Muller法:正規乱数 $Z \sim N(0,1)$ を一様乱数にスケーリング

💻 コード全文(比較付き)

# Program Name: fireblast_dual_rng_comparison.py
# Creation Date: 20250602
# Overview: Compare high-precision RNG and Box-Muller method for Fire Blast hit/miss simulation
# Usage: Run in Python environment with matplotlib and numpy installed

import numpy as np
import matplotlib.pyplot as plt

# パラメータ設定 / Define parameters
n_trials = 10000           # 試行回数 / Number of simulations
hit_rate = 0.85            # 命中率 / Hit rate

# --- 方法1: default_rng() による一様乱数 / High-precision RNG (recommended) ---
rng = np.random.default_rng()
uniform_random_1 = rng.random(n_trials)
hits_rng = uniform_random_1 < hit_rate
hit_count_rng = np.sum(hits_rng)

# --- 方法2: Box-Muller法で正規分布→スケーリング / Box-Muller method ---
U1 = np.random.rand(n_trials)
U2 = np.random.rand(n_trials)
Z1 = np.sqrt(-2 * np.log(U1)) * np.cos(2 * np.pi * U2)
uniform_random_2 = (Z1 - np.min(Z1)) / (np.max(Z1) - np.min(Z1))
hits_bm = uniform_random_2 < hit_rate
hit_count_bm = np.sum(hits_bm)

# --- 結果表示 / Print results ---
print(f"[default_rng] Hit: {hit_count_rng}  ({hit_count_rng / n_trials:.4f})")
print(f"[Box-Muller ] Hit: {hit_count_bm}  ({hit_count_bm / n_trials:.4f})")

# --- グラフ描画 / Plot results ---
labels = ['Hit', 'Miss']
values_rng = [hit_count_rng, n_trials - hit_count_rng]
values_bm = [hit_count_bm, n_trials - hit_count_bm]

fig, axs = plt.subplots(1, 2, figsize=(12, 5))

axs[0].bar(labels, values_rng, color=['orange', 'gray'])
axs[0].set_title('Fire Blast Hit (default_rng)')
axs[0].set_ylabel('Number of times')
axs[0].grid(axis='y')

axs[1].bar(labels, values_bm, color=['orange', 'gray'])
axs[1].set_title('Fire Blast Hit (Box-Muller)')
axs[1].grid(axis='y')

plt.suptitle('Comparison of RNG Methods for Fire Blast (Accuracy = 85%)')
plt.tight_layout()
plt.show()

📊 出力例

image.png


🔍 精度の違いについて

比較項目 default_rng() Box-Muller法
原理 高精度一様乱数(Mersenne Twister等) 正規分布を一様に変換
実装の簡便さ △(変換とスケーリングが必要)
分布の理論保証 一様分布に対し直接評価 範囲の切断や正規分布外の誤差あり
実用性 本番シミュレーションに最適 教育用・理論確認に最適

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?