8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/securer_randomer/rand.rb', line 8
def self.rand(n = 0, emulate_kernel = false)
if n.is_a?(Range)
unless n.begin.is_a?(Numeric) and n.end.is_a?(Numeric)
if emulate_kernel and defined?(JRUBY_VERSION)
raise NoMethodError, "undefined method `-' for \"#{n.end}\""
else
raise TypeError, 'no implicit conversion of Range into Fixnum'
end
end
if n.end < n.begin
if emulate_kernel
raise ArgumentError, "invalid argument - #{n}" if defined?(JRUBY_VERSION)
nil
else
m = Range.new(n.end, n.begin, false)
while true q = _rand_range(m)
break q unless n.exclude_end? and q == n.end
end
end
elsif n.begin == n.end and n.exclude_end?
raise ArgumentError, "invalid argument - #{n}" if emulate_kernel and defined?(JRUBY_VERSION)
nil
else
_rand_range(n)
end
else
raise TypeError, "no implicit conversion of #{n.class} into Fixnum" \
unless n.nil? or n.is_a?(Numeric)
if n.nil? or n.zero?
_randex
elsif emulate_kernel
_rand_range(Range.new(0, n.to_i.abs, true))
else
_rand_range(Range.new(0, n.abs, true)) * (n < 0 ? -1 : 1)
end
end
end
|