Sha256: 39c462547fcf7e106676bb92f9af8a88eff29f2a94a8013efa56220d6d34f9b4

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require "simpack/version"

module Simpack

  class LCG

    attr_reader :multiplier, :modulus, :increment, :seed

    def initialize(options = {})
      default_options = {multiplier: 6364136223846793005,
                         modulus: 2 ** 64,
                         increment: 1442695040888963407,
                         seed: Time.now.to_i}

      options = default_options.merge(options)
      check_options(options)

      @multiplier = options[:multiplier]
      @modulus = options[:modulus]
      @increment = options[:increment]
      @seed = @x = options[:seed]
    end

    def uniform(n = 1)
      return generate_number if n == 1

      results = Array.new(n)
      (0...n).each do |i|
        results[i] = generate_number
      end
      results
    end

    private

    def check_options(options)
      raise "Invalid Modulus" if options[:modulus] == 0
      raise "Invalid Multiplier" if options[:multiplier] == 0
    end

    def generate_number
      @x = (@multiplier * @x + @increment) % @modulus
      @x / @modulus.to_f
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simpack-0.1.0 lib/simpack.rb