123456789_123456789_123456789_123456789_123456789_

Module: Concurrent::ThreadSafe::Util::XorShiftRandom

Overview

A xorshift random number (positive Fixnums) generator, provides reasonably cheap way to generate thread local random numbers without contending for the global Kernel.rand.

Usage:

x = XorShiftRandom.get # uses Kernel.rand to generate an initial seed
while true
  if (x = XorShiftRandom.xorshift).odd? # thread-localy generate a next random number
    do_something_at_random
  end
end

Constant Summary

Instance Method Summary

  • #get

    Generates an initial non-zero positive Fixnum via Kernel.rand.

  • #xorshift(x)

    using the “yˆ=y>>a; yˆ=y<<b; yˆ=y>>c;” transform with the (a,b,c) tuple with values (1,1,54) to minimise Bignum overflows.

Instance Method Details

#get

Generates an initial non-zero positive Fixnum via Kernel.rand.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb', line 27

def get
  Kernel.rand(MAX_XOR_SHIFTABLE_INT) + 1 # 0 can't be xorshifted
end

#xorshift(x)

using the “yˆ=y>>a; yˆ=y<<b; yˆ=y>>c;” transform with the (a,b,c) tuple with values (1,1,54) to minimise Bignum overflows

See additional method definition at line 34.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb', line 41

def xorshift(x)
  x ^= x >> 3
  x ^= (x << 1) & MAX_INT # cut-off Bignum overflow
  x ^= x >> 14
end