Class: RailsDataExplorer::Statistics::RngPowerLaw

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_data_explorer/statistics/rng_power_law.rb

Overview

Responsibilities:

* Provide random numeric data, following a power distribution.

Instance Method Summary collapse

Constructor Details

#initialize(min = 1, max = 1000, pow = 2, rng = lambda { Kernel.rand }) ⇒ RngPowerLaw

Returns a new instance of RngPowerLaw.

Parameters:

  • min (Numeric) (defaults to: 1)
  • max (Numeric) (defaults to: 1000)
  • pow (Numeric) (defaults to: 2)
  • rng (Proc, optional) (defaults to: lambda { Kernel.rand })

    a random number generator



15
16
17
18
# File 'lib/rails_data_explorer/statistics/rng_power_law.rb', line 15

def initialize(min = 1, max = 1000, pow = 2, rng = lambda { Kernel.rand })
  @min, @max, @pow, @rng = min, max, pow, rng
  @max += 1
end

Instance Method Details

#randObject

Returns random data following a power distribution.



21
22
23
24
25
26
27
28
# File 'lib/rails_data_explorer/statistics/rng_power_law.rb', line 21

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