Created
February 21, 2013 08:49
-
-
Save komasaru/5003310 to your computer and use it in GitHub Desktop.
Ruby script to calc uniform random numbers by LCGs 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
# -*- coding: utf-8 -*- | |
class RndnumLcgs | |
# 各種定数定義 | |
A = 1103515245 # 乗数 | |
C = 12345 # 加数 | |
M = 2 ** 31 # 法 | |
N = 1000 # 発生させる乱数の個数 | |
# 計算クラス | |
class Calc | |
# コンストラクタ | |
def initialize | |
@r = 12345 # 乱数の種の初期値 | |
end | |
# 一様乱数生成 | |
def generate_rndnum | |
# ループしながら漸化式を計算 | |
0.upto( N ) do |i| | |
@r = (A * @r + C) % M | |
printf("%10d ", @r) | |
print "\n" if i % 5 == 4 | |
end | |
end | |
end | |
# メイン処理 | |
begin | |
# 計算クラスインスタンス化 | |
obj_calc = Calc.new | |
# 一様乱数生成 | |
obj_calc.generate_rndnum | |
rescue => e | |
# エラーメッセージ | |
puts "[例外発生] #{e}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment