Class: Teebo::Number

Inherits:
Object
  • Object
show all
Defined in:
lib/teebo/number.rb

Overview

Helper for generating numbers according to various distributions.

Class Method Summary collapse

Class Method Details

.benford_dist(upper_bound, decimals = 0) ⇒ Object

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



23
24
25
# 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 }) ⇒ Object

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



12
13
14
15
16
17
# 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