Last active
April 19, 2018 04:31
-
-
Save komasaru/5526b796c2e4196ea6d84bda959caf6e to your computer and use it in GitHub Desktop.
Ruby script to compute random number with LGGs algorithm.
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
#! /usr/local/bin/ruby | |
#****************************** | |
# 線形合同法による一様乱数生成 | |
#****************************** | |
class RndnumLcgs | |
# 各種定数定義 | |
A = 1103515245 # 乗数 | |
C = 12345 # 加数 | |
M = 2 ** 31 # 法 | |
N = 1000 # 発生させる乱数の個数 | |
# コンストラクタ | |
def initialize | |
@r = C # 乱数の種の初期値 | |
end | |
# 一様乱数生成 | |
def generate_rndnum | |
0.upto( N ) do |i| | |
@r = (A * @r + C) % M | |
printf("%10d ", @r) | |
print "\n" if i % 5 == 4 | |
end | |
print "\n" | |
rescue => e | |
raise | |
end | |
end | |
if __FILE__ == $0 | |
begin | |
RndnumLcgs.new.generate_rndnum | |
rescue => e | |
$stderr.puts "[例外発生] #{e}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment