Permalink
Browse files

Merge pull request #6 from cschwartz/master

Added the possibility to pass an existing rng to Exponential
  • Loading branch information...
2 parents 042b51e + 8fb12d6 commit bed44beec1f8d8ea34103d64c795041f8ab506d4 @clbustos committed Jul 31, 2012
Showing with 20 additions and 5 deletions.
  1. +5 −4 lib/distribution/exponential/ruby.rb
  2. +15 −1 spec/exponential_spec.rb
@@ -2,10 +2,11 @@ module Distribution
module Exponential
module Ruby_
class << self
- def rng(l)
- lambda {p_value(rand(),l)}
- end
- def pdf(x,l)
+ def rng(l, opts = {})
+ rng = opts[:random] || Random
+ lambda {p_value(rng.rand,l)}
+ end
+ def pdf(x,l)
return 0 if x<0
l*Math.exp(-l*x)
end
View
@@ -76,5 +76,19 @@
#
# end
# end
-
+ describe "rng" do
+ it "should default to Kernel#rand if no :random is given" do
+ Random.stub!(:rand)
+ Random.should_receive(:rand).and_return (0.5)
+ rng = Distribution::Exponential.rng 1.0
+ rng.call()
+ end
+
+ it "should use a given rng if one is passed during construction" do
+ random = double("random")
+ random.should_receive(:rand).and_return(0.5)
+ rng = Distribution::Exponential::rng 1.0, :random => random
+ rng.call()
+ end
+ end
end

0 comments on commit bed44be

Please sign in to comment.