Module: ThreadSafe::Util::XorShiftRandom
- Extended by:
- XorShiftRandom
- Included in:
- XorShiftRandom
- Defined in:
- lib/thread_safe/util/xor_shift_random.rb
Overview
A xorshift random number (positive +Fixnum+s) 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 collapse
- MAX_XOR_SHIFTABLE_INT =
MAX_INT - 1
Class Method Summary collapse
-
.get ⇒ Object
Generates an initial non-zero positive +Fixnum+ via +Kernel.rand+.
-
.xorshift(x) ⇒ Object
using the "yˆ=y>>a; yˆ=y<>c;" transform with the (a,b,c) tuple with values (1,1,54) to minimise Bignum overflows.
Instance Method Summary collapse
-
#get ⇒ Object
Generates an initial non-zero positive +Fixnum+ via +Kernel.rand+.
-
#xorshift(x) ⇒ Object
using the "yˆ=y>>a; yˆ=y<>c;" transform with the (a,b,c) tuple with values (1,1,54) to minimise Bignum overflows.
Class Method Details
.get ⇒ Object
Generates an initial non-zero positive +Fixnum+ via +Kernel.rand+.
19 20 21 |
# File 'lib/thread_safe/util/xor_shift_random.rb', line 19 def get Kernel.rand(MAX_XOR_SHIFTABLE_INT) + 1 # 0 can't be xorshifted end |
.xorshift(x) ⇒ Object
using the "yˆ=y>>a; yˆ=y<>c;" transform with the (a,b,c) tuple with values (1,1,54) to minimise Bignum overflows
26 27 28 29 30 |
# File 'lib/thread_safe/util/xor_shift_random.rb', line 26 def xorshift(x) x ^= x >> 3 x ^= (x << 1) & MAX_INT # cut-off Bignum overflow x ^= x >> 14 end |
Instance Method Details
#get ⇒ Object
Generates an initial non-zero positive +Fixnum+ via +Kernel.rand+.
19 20 21 |
# File 'lib/thread_safe/util/xor_shift_random.rb', line 19 def get Kernel.rand(MAX_XOR_SHIFTABLE_INT) + 1 # 0 can't be xorshifted end |
#xorshift(x) ⇒ Object
using the "yˆ=y>>a; yˆ=y<>c;" transform with the (a,b,c) tuple with values (1,1,54) to minimise Bignum overflows
26 27 28 29 30 |
# File 'lib/thread_safe/util/xor_shift_random.rb', line 26 def xorshift(x) x ^= x >> 3 x ^= (x << 1) & MAX_INT # cut-off Bignum overflow x ^= x >> 14 end |