class Teebo::Number

Helper for generating numbers according to various distributions.

Public Class Methods

benford_dist(upper_bound, decimals=0) click to toggle source

Generates a number according to Benford's law, meaning that it is more indicative of numbers encountered in real life.

# File lib/teebo/number.rb, line 23
def self.benford_dist(upper_bound, decimals=0)
  (upper_bound**Random.rand).round(decimals)
end
normal_dist(mean, std_deviation, rand = lambda { Kernel.rand }) click to toggle source

Generates a number using a normal distribution.

A basic implementation of the Box-Muller transform. Adapted from an answer by antonakos on Stack Overflow here: stackoverflow.com/questions/5825680

# File lib/teebo/number.rb, line 12
def self.normal_dist(mean, std_deviation, rand = lambda { Kernel.rand })
  theta = 2 * Math::PI * rand.call
  rho = Math.sqrt(-2 * Math.log(1 - rand.call))
  scale = std_deviation * rho
  mean + scale * Math.cos(theta)
end