### Home Classes Methods

### In Files

- lib/thread_safe/util/xor_shift_random.rb

### Methods

- #get
- #xorshift

### Class/Module Index

Quicksearch

- ThreadSafe
- ThreadSafe::Array
- ThreadSafe::AtomicReferenceCacheBackend
- ThreadSafe::AtomicReferenceCacheBackend::Node
- ThreadSafe::AtomicReferenceCacheBackend::Table
- ThreadSafe::Cache
- ThreadSafe::Hash
- ThreadSafe::MriCacheBackend
- ThreadSafe::NonConcurrentCacheBackend
- ThreadSafe::SynchronizedCacheBackend
- ThreadSafe::Util
- ThreadSafe::Util::Adder
- ThreadSafe::Util::CheapLockable
- ThreadSafe::Util::PowerOfTwoTuple
- ThreadSafe::Util::Striped64
- ThreadSafe::Util::Striped64::Cell
- ThreadSafe::Util::Volatile
- ThreadSafe::Util::VolatileTuple
- ThreadSafe::Util::XorShiftRandom
- SynchronizedDelegator
- Threadsafe

No matching classes.

# ThreadSafe::Util::XorShiftRandom

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

### Constants

MAX_XOR_SHIFTABLE_INT

### Public Instance Methods

get() click to toggle source

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

    # 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) click to toggle source

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

    # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.