Skip to content
Browse files

Bound 'scale' parameter between 0 and 1

  • Loading branch information...
1 parent a5f8889 commit 84974d70ccdc6d423af3f0d86cea4e78ec13acbb @kputnam committed
Showing with 20 additions and 28 deletions.
  1. +20 −28 lib/propr/monad/random.rb
View
48 lib/propr/monad/random.rb
@@ -10,11 +10,12 @@ class << Random
#############################################
def run(computation, scale = BigDecimal(1))
- computation.call(scale)
+ computation.call(bound scale)
end
def eval(computation, scale = BigDecimal(1), retries = 0)
skipped = 0
+ scale = bound(scale)
while true
value, _, success = computation.call(scale)
@@ -55,36 +56,15 @@ def guard(*conditions)
end
end
- # When given zero arguments, returns the current scale factor
- #
- # When given one argument, sets the current scale factor to
- # the given value, bounded between 0..1.
- #
# When given two arguments, scales a numeric value around a
# given origin `ref`, using the current scale factor (0..1).
#
- def scale(number = nil, range = nil, ref = nil)
- # if range.nil?
- # if number.nil?
- # lambda do |scale|
- # # Get scale factor
- # [scale, scale, true]
- # end
- # else
- # # Set scale factor 0..1
- # lambda do |scale|
- # scale = scale.coerce(1)[0] if scale > 1
- # scale = scale.coerce(0)[0] if scale < 0
- # [nil, scale, true]
- # end
- # end
- # else
- lambda do |scale|
- pct = (number - ref) / range
- rng_ = (range ** scale) - 1 + scale
- [ref + rng_* pct, scale, true]
- end
- # end
+ def scale(number, range, ref)
+ lambda do |scale|
+ pct = (number - ref) / range
+ rng_ = (range ** scale) - 1 + scale
+ [ref + rng_* pct, scale, true]
+ end
end
# Generate psuedo-random number normally distributed between
@@ -96,5 +76,17 @@ def rand(limit = nil)
end
end
+ private
+
+ def bound(scale)
+ if scale > 1
+ scale.coerce(1)[0]
+ elsif scale < 0
+ scale.coerce(0)[0]
+ else
+ scale
+ end
+ end
+
end
end

0 comments on commit 84974d7

Please sign in to comment.
Something went wrong with that request. Please try again.