Skip to content

Instantly share code, notes, and snippets.

@kozakana
Last active August 29, 2015 13:59
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 kozakana/10586822 to your computer and use it in GitHub Desktop.
Save kozakana/10586822 to your computer and use it in GitHub Desktop.
Rubyで使用されている乱数の均等分布について(メルセンヌ・ツイスタ) ref: http://qiita.com/kozakana/items/b5229094cf8d29767847
MAX_VAL = 101
REP_CNT = 10**6
sum = 0.0
prng = Random.new(Random.new_seed)
(0...REP_CNT).each{|idx|
p sum = (sum*idx + prng.rand(MAX_VAL))/(idx+1)
}
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'gruff'
FONT = "/Library/Fonts/Osaka.ttf"
MAX_VAL = 101
GRAPH_NUM = 7
REP_CNT = 10**GRAPH_NUM
graph_point = Array.new(GRAPH_NUM)
avg, rec_cnt = 0.0, 0
prng = Random.new(Random.new_seed)
(0...REP_CNT).each{|idx|
avg = (avg*idx + prng.rand(MAX_VAL))/(idx+1)
if 10**rec_cnt-1 == idx
graph_point[rec_cnt] = avg
rec_cnt += 1
end
}
p graph_point
g = Gruff::Line.new
g.font = FONT
(0..graph_point.length).each{|idx|
g.labels[idx] = "10^#{idx}"
}
g.data("Mersenne twister", graph_point)
g.write('MT.png')
実際の値は
10^0:67.0
10^1:55.5
10^2:52.39
10^3:51.352
10^4:50.076400000000085
10^5:49.93751999999963
10^6:49.99161700000036
10^7:49.99675620000007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment