Skip to content

Commit

Permalink
Allow to generate floats using normal distribution
Browse files Browse the repository at this point in the history
This allow us to generate points with more probability when we are
closer to a limit/key point.

The sum of 6 draws from a uniform distribution give as a draw of a
normal distribution centered in 3. This is commonly used in programming
for simplicity and it is proved by the central limit theorem.

Contributes to rantly-rb#29

Pair-programmed by @Ana06 and @vicgalle.
  • Loading branch information
Ana06 committed Feb 10, 2018
1 parent 70726f9 commit 009b9a3
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rantly/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ def positive_integer
range(0)
end

def float
rand
def float(distribution=nil, center=0)
case distribution
when :normal
# Sum of 6 draws from a uniform distribution give as a draw of a normal
# distribution centered in 3 (central limit theorem).
[rand, rand, rand, rand, rand, rand].reduce(0, :+) - 3 + center
else
rand
end
end

def range(lo=nil,hi=nil)
Expand Down

0 comments on commit 009b9a3

Please sign in to comment.