Xoshiro256

A small, dependency-free, pure-Ruby implementation of xoshiro256++.

It follows the reference implementation by David Blackman and Sebastiano Vigna and uses SplitMix64 to expand a 64-bit seed into the generator’s 256-bit state.

xoshiro256++ is a fast, high-quality non-cryptographic PRNG. Do not use it for keys, tokens, passwords, salts or other security-sensitive randomness.

Installation

Install as a gem

shell $ gem install xoshiro256

Or add it to your Gemfile and execute bundle install

ruby gem "xoshiro256"

Usage

```ruby require “xoshiro256”

rng = Xoshiro256::Random.new(1234)

rng.next_u64 # raw 64-bit unsigned integer rng.rand # Float in [0, 1) rng.rand(100) # Integer in 0…100 rng.rand(2.5) # Float in [0, 2.5) rng.rand(10..20) # Integer in 10..20 rng.bytes(32) # 32 random bytes ```

The same seed always produces the same stream:

```ruby a = Xoshiro256::Random.new(42) b = Xoshiro256::Random.new(42)

a.next_u64 == b.next_u64 # => true ```

To restore an exact internal state or exchange states with another xoshiro256++ implementation:

ruby rng = Xoshiro256::Random.from_state(1, 2, 3, 4) rng.state # => [1, 2, 3, 4] (a frozen snapshot) rng.next_u64 # => 0x0000000002800001 rng.seed # => nil (raw state has no originating seed)

from_state requires exactly four Integer words in 0..0xffffffffffffffff and rejects the all-zero state. It preserves the words directly, bypassing SplitMix64.

Shuffling and sampling

Array#shuffle, Array#shuffle! and Array#sample accept any object that responds to rand through their random: keyword, so a generator can be passed in place of Ruby’s Random:

```ruby rng = Xoshiro256::Random.new(1234)

deck = (1..52).to_a.shuffle(random: rng) hand = deck.sample(5, random: rng) ```

Ruby shuffles with Fisher-Yates: walking the array from the end, it calls rng.rand(n) for each position and swaps the element with the one at the returned index. The result depends only on the generator’s stream, so the same seed always produces the same order:

ruby %w[a b c d e].shuffle(random: Xoshiro256::Random.new(42)) # => ["c", "e", "d", "a", "b"]

Parallel streams

jump advances the state as if 2^128 values had been generated, while long_jump advances it as if 2^192 values had been generated.

ruby worker_a = Xoshiro256::Random.new(42) worker_b = Xoshiro256::Random.new(42).jump worker_c = Xoshiro256::Random.new(42).long_jump

These operations are useful for creating deterministic, non-overlapping streams for parallel computations. Instances are mutable; prefer one generator per thread or worker instead of sharing an instance concurrently.

Contributing

Bug reports and pull requests are welcome on GitHub.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Code of Conduct.

You can contribute changes by forking the project and submitting a pull request. To get started:

  1. Fork the repo
  2. Install the dependencies (bin/setup)
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Pass the test and lint suite (bundle exec rake test)
  6. Push to the branch (git push origin my-new-feature)
  7. Create a new pull request

For more information about contributing to this project, check out CONTRIBUTING.

License

This project is released as open source under the terms of the MIT License.