Last active
August 29, 2015 13:59
-
-
Save kozakana/10586822 to your computer and use it in GitHub Desktop.
Rubyで使用されている乱数の均等分布について(メルセンヌ・ツイスタ) ref: http://qiita.com/kozakana/items/b5229094cf8d29767847
This file contains hidden or 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
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) | |
} |
This file contains hidden or 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
# -*- 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') |
This file contains hidden or 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
実際の値は | |
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