class RailsDataExplorer::Statistics::RngPowerLaw

Responsibilities:

* Provide random numeric data, following a power distribution.

Public Class Methods

new(min = 1, max = 1000, pow = 2, rng = lambda { Kernel.rand }) click to toggle source

@param min [Numeric] @param max [Numeric] @param pow [Numeric] @param rng [Proc, optional] a random number generator

# File lib/rails_data_explorer/statistics/rng_power_law.rb, line 14
def initialize(min = 1, max = 1000, pow = 2, rng = lambda { Kernel.rand })
  @min, @max, @pow, @rng = min, max, pow, rng
  @max += 1
end

Public Instance Methods

rand() click to toggle source

Returns random data following a power distribution.

# File lib/rails_data_explorer/statistics/rng_power_law.rb, line 20
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