Created
February 21, 2013 08:45
-
-
Save komasaru/5003295 to your computer and use it in GitHub Desktop.
Ruby script to calc normal random numbers by box-muller method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#********************************************* | |
# ボックス=ミューラー法法による正規乱数生成 | |
#********************************************* | |
class RndnumBoxMuller | |
# 各種定数 | |
M = 10 # 平均 | |
S = 2.5 # 標準偏差 | |
N = 10000 # 発生させる乱数の個数 | |
PI = 3.1415926535 # 円周率 | |
SCALE = N / 100.0 # ヒストグラム用スケール | |
# 計算クラス | |
class Calc | |
# コンストラクタ | |
def initialize | |
# 件数格納用配列初期化 | |
@hist = Array.new( M * 5, 0 ) | |
end | |
# 正規乱数生成 | |
def generate_rndnum | |
# N 回乱数生成処理を繰り返す | |
0.upto( N - 1 ) do |i| | |
# 整数乱数を2個生成 | |
res = rnd | |
# 整数乱数をカウント | |
@hist[res[0]] += 1 | |
@hist[res[1]] += 1 | |
end | |
end | |
# 整数乱数 | |
def rnd | |
# 一様乱数 | |
# ( (0,1] の実数乱数 ) | |
r_1 = rand | |
r_2 = rand | |
# 正規乱数計算 | |
x = S * Math.sqrt(-2 * Math.log(r_1)) * Math.cos(2 * PI * r_2) + M | |
y = S * Math.sqrt(-2 * Math.log(r_1)) * Math.sin(2 * PI * r_2) + M | |
# 2個の正規乱数を整数化して配列でリターン | |
return [ x.to_i, y.to_i ] | |
end | |
# 結果表示 | |
def display | |
# 0 ~ M * 2 を表示 | |
0.upto( M * 2 ) do |i| | |
# 件数表示 | |
printf("%3d:%4d | ", i, @hist[i]) | |
# ヒストグラム表示 | |
1.upto( @hist[i] / SCALE ) { |j| print "*" } | |
puts | |
end | |
end | |
end | |
# メイン処理 | |
begin | |
# 計算クラスインスタンス化 | |
obj_calc = Calc.new | |
# 正規乱数生成 | |
obj_calc.generate_rndnum | |
# 結果表示 | |
obj_calc.display | |
rescue => e | |
# エラーメッセージ | |
puts "[例外発生] #{e}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment