Sha256: 4f73d72b0938dad3f623d8a03ce303a95b489cc915810698ed933b6cb6b8857c

Contents?: true

Size: 800 Bytes

Versions: 5

Compression:

Stored size: 800 Bytes

Contents

# -*- coding: utf-8 -*-

class RailsDataExplorer
  module Statistics

    # Responsibilities:
    #  * Provide random numeric data, following a power distribution.
    #
    class RngPowerLaw

      # @param min [Numeric]
      # @param max [Numeric]
      # @param pow [Numeric]
      # @param rng [Proc, optional] a random number generator
      def initialize(min = 1, max = 1000, pow = 2, rng = lambda { Kernel.rand })
        @min, @max, @pow, @rng = min, max, pow, rng
        @max += 1
      end

      # Returns random data following a power distribution.
      def rand
        y = (
          (
            (@max ** (@pow + 1) - @min ** (@pow + 1)) * @rng.call + @min ** (@pow + 1)
          ) ** (1.0 / (@pow + 1))
        ).to_i
        (@max - 1 - y) + @min
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails-data-explorer-1.0.4 lib/rails_data_explorer/statistics/rng_power_law.rb
rails-data-explorer-1.0.3 lib/rails_data_explorer/statistics/rng_power_law.rb
rails-data-explorer-1.0.2 lib/rails_data_explorer/statistics/rng_power_law.rb
rails-data-explorer-1.0.1 lib/rails_data_explorer/statistics/rng_power_law.rb
rails-data-explorer-1.0.0 lib/rails_data_explorer/statistics/rng_power_law.rb