randomfromrange 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/randomfromrange.rb +8 -4
  2. metadata +1 -1
@@ -5,10 +5,14 @@ Kernel.class_eval do
5
5
  if(v.respond_to? :to_i)
6
6
  return old_rand(v.to_i.abs)
7
7
  elsif(v.respond_to? :begin and v.respond_to? :end)
8
- b = (v.begin.to_s =~ /^[0-9]+\.([0-9]+)$/) ? $1.to_s : ''
9
- e = (v.end.to_s =~ /^[0-9]+\.([0-9]+)$/) ? $1.to_s : ''
10
- precision = [b.length, e.length].max
11
- return ((v.end-v.begin)*old_rand() + v.begin).round(precision)
8
+ first = nil
9
+ v.step(1) { |n| first = n }
10
+ precision = ((first.to_s =~ /^[0-9]+\.([0-9]+)$/) ? $1.to_s : '').length
11
+ if precision > 0
12
+ return (('%.' + precision.to_s + 'f') % ((v.end-v.begin)*old_rand() + v.begin)).to_f
13
+ else
14
+ return ((v.end-v.begin)*old_rand() + v.begin).round
15
+ end
12
16
  else
13
17
  raise TypeError, 'can\'t convert ' + v.class + ' into Integer'
14
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: randomfromrange
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: