Sha256: c629f5c110f63318a59f4c9df4d951f25b9ea7227776660212932be76e8eb5d9

Contents?: true

Size: 1.4 KB

Versions: 139

Compression:

Stored size: 1.4 KB

Contents

module ThreadSafe
  module Util
    # 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
    module XorShiftRandom
      extend self
      MAX_XOR_SHIFTABLE_INT = MAX_INT - 1

      # Generates an initial non-zero positive +Fixnum+ via +Kernel.rand+.
      def get
        Kernel.rand(MAX_XOR_SHIFTABLE_INT) + 1 # 0 can't be xorshifted
      end

      # xorshift based on: http://www.jstatsoft.org/v08/i14/paper
      if 0.size == 4
        # using the "yˆ=y>>a; yˆ=y<<b; yˆ=y>>c;" transform with the (a,b,c) tuple with values (3,1,14) to minimise Bignum overflows
        def xorshift(x)
          x ^= x >> 3
          x ^= (x << 1) & MAX_INT # cut-off Bignum overflow
          x ^= x >> 14
        end
      else
        # 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
        def xorshift(x)
          x ^= x >> 1
          x ^= (x << 1) & MAX_INT # cut-off Bignum overflow
          x ^= x >> 54
        end
      end
    end
  end
end

Version data entries

139 entries across 131 versions & 45 rubygems

Version Path
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/util/xor_shift_random.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/util/xor_shift_random.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/thread_safe-0.3.5-java/lib/thread_safe/util/xor_shift_random.rb
logstash-codec-json-2.0.3 vendor/gems/thread_safe-0.3.5-java/lib/thread_safe/util/xor_shift_random.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/util/xor_shift_random.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/thread_safe-0.3.5-java/lib/thread_safe/util/xor_shift_random.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/thread_safe-0.3.5/lib/thread_safe/util/xor_shift_random.rb
thread_safe-0.3.5-java lib/thread_safe/util/xor_shift_random.rb
thread_safe-0.3.5 lib/thread_safe/util/xor_shift_random.rb