Class: Keisan::Functions::Rand

Inherits:
ProcFunction show all
Defined in:
lib/keisan/functions/rand.rb

Instance Attribute Summary

Attributes inherited from ProcFunction

#function_proc

Attributes inherited from Keisan::Function

#arity, #name

Instance Method Summary collapse

Methods inherited from ProcFunction

#evaluate, #simplify, #value

Methods inherited from Keisan::Function

#differentiate, #evaluate, #simplify, #value

Constructor Details

#initializeRand

Returns a new instance of Rand.



4
5
6
7
# File 'lib/keisan/functions/rand.rb', line 4

def initialize
  @name = "rand"
  @arity = ::Range.new(1,2)
end

Instance Method Details

#call(context, *args) ⇒ Object

Single argument: integer in range [0, max) Double argument: integer in range [min, max)



11
12
13
14
15
16
17
18
19
20
# File 'lib/keisan/functions/rand.rb', line 11

def call(context, *args)
  case args.size
  when 1
    context.random.rand(args.first)
  when 2
    context.random.rand(args.first...args.last)
  else
    raise Exceptions::InvalidFunctionError.new
  end
end