Class: Distrb::Normal::BoxMullerTransform
- Inherits:
-
Object
- Object
- Distrb::Normal::BoxMullerTransform
- Defined in:
- lib/distrb/normal/box_muller_transform.rb
Overview
Box-Muller transform to sample from Normal distribution See: en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
Instance Method Summary collapse
-
#initialize ⇒ BoxMullerTransform
constructor
A new instance of BoxMullerTransform.
- #sample ⇒ Object
Constructor Details
#initialize ⇒ BoxMullerTransform
Returns a new instance of BoxMullerTransform.
8 9 10 |
# File 'lib/distrb/normal/box_muller_transform.rb', line 8 def initialize @uniform = Distrb::Uniform.new end |
Instance Method Details
#sample ⇒ Object
12 13 14 15 16 |
# File 'lib/distrb/normal/box_muller_transform.rb', line 12 def sample x = @uniform.sample y = @uniform.sample Math.sqrt(-2 * Math.log(x)) * Math.cos(2 * Math::PI * y) end |