Sha256: 449a79711ae59544d350fa0e3f582958d1067ae524f67dc08a8bd79779fb3d5d

Contents?: true

Size: 1.59 KB

Versions: 312

Compression:

Stored size: 1.59 KB

Contents

require 'concurrent/thread_safe/util'

module Concurrent

  # @!visibility private
  module ThreadSafe

    # @!visibility private
    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
end

Version data entries

312 entries across 287 versions & 56 rubygems

Version Path
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.45 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.44 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.43 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.38 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.1.10/lib/concurrent-ruby/concurrent/thread_safe/util/xor_shift_random.rb