Class: RailsDataExplorer::Statistics::RngPowerLaw

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-data-explorer/statistics/rng_power_law.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RngPowerLaw.



5
6
7
8
# File 'lib/rails-data-explorer/statistics/rng_power_law.rb', line 5

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



10
11
12
13
14
15
16
17
# File 'lib/rails-data-explorer/statistics/rng_power_law.rb', line 10

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