Last active
August 29, 2015 14:08
-
-
Save rutan/183573e442a140765538 to your computer and use it in GitHub Desktop.
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
# 128bit Xorshift | |
class Xor128 | |
MASK = 0xffffffff | |
# 初期化 | |
# @param x [Integer] シード1 | |
# @param y [Integer] シード2 | |
# @param z [Integer] シード3 | |
# @param w [Integer] シード4 | |
def initialize(x = 123456789, y = 362436069, z = 521288629, w = 88675123) | |
@x = x & MASK | |
@y = y & MASK | |
@z = z & MASK | |
@w = w & MASK | |
end | |
# 乱数の生成 | |
# @return [Integer] 0〜4294967295の値 | |
def rand | |
t = @x ^ (@x << 11 & MASK) & MASK | |
@x = @y; @y = @z; @z = @w | |
@w = ((@w ^ (@w >> 19 & MASK) & MASK) ^ (t ^ (t >> 8 & MASK) & MASK)) & MASK | |
end | |
end | |
xor = Xor128.new | |
10.times { puts xor.rand } | |
# => 3701687786 | |
# => 458299110 | |
# => 2500872618 | |
# => 3633119408 | |
# => 516391518 | |
# => 2377269574 | |
# => 2599949379 | |
# => 717229868 | |
# => 137866584 | |
# => 395339113 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment