Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created February 21, 2013 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/5003310 to your computer and use it in GitHub Desktop.
Save komasaru/5003310 to your computer and use it in GitHub Desktop.
Ruby script to calc uniform random numbers by LCGs method.
# -*- 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