class FifthedSim::MultiNode
This class models the result of a roll of multiple dice. It is filled with the actual result of randomly-rolled dice, but contains methods to enable the calculation of average values.
Public Class Methods
d(num, type)
click to toggle source
# File lib/fifthed_sim/nodes/multi_node.rb, line 32 def self.d(num, type) self.new(num.times.map{RollNode.roll(type)}) end
new(array)
click to toggle source
Generally, don't calculate this yourself
# File lib/fifthed_sim/nodes/multi_node.rb, line 38 def initialize(array) unless array.is_a?(Array) && ! array.empty? raise ArgumentError, "Not a valid array" end unless array.all?{|elem| elem.is_a?(RollNode) } raise ArgumentError, "Not all die rolls" end @array = array end
Public Instance Methods
average()
click to toggle source
What is the theoretical average value when we roll this many dice?
# File lib/fifthed_sim/nodes/multi_node.rb, line 66 def average @array.map(&:average).inject(:+) end
dice_type()
click to toggle source
What kind of dice did we roll?
# File lib/fifthed_sim/nodes/multi_node.rb, line 84 def dice_type @array.first.type end
distribution()
click to toggle source
Obtain a probability distribution for when we roll this many dice. This is an instnace of the Distribution
class.
# File lib/fifthed_sim/nodes/multi_node.rb, line 103 def distribution total_possible = (dice_type ** roll_count) mapped = min_value.upto(max_value).map do |k| [k, (occurences(k) / total_possible.to_f)] end Distribution.new(Hash[mapped]) end
expression_equation()
click to toggle source
# File lib/fifthed_sim/nodes/multi_node.rb, line 117 def expression_equation "(" + @array.map do |a| a.expression_equation end.join(", ") + ")" end
has_crit?()
click to toggle source
Did any of our dice crit?
# File lib/fifthed_sim/nodes/multi_node.rb, line 54 def has_crit? @array.any?(&:crit?) end
has_critfail?()
click to toggle source
Did any of our dice critically fail?
# File lib/fifthed_sim/nodes/multi_node.rb, line 60 def has_critfail? @array.any?(&:critfail?) end
max_value()
click to toggle source
The maximum value we could have rolled
# File lib/fifthed_sim/nodes/multi_node.rb, line 96 def max_value dice_type * roll_count end
min_value()
click to toggle source
The minimum value we could have rolled
# File lib/fifthed_sim/nodes/multi_node.rb, line 90 def min_value roll_count end
reroll()
click to toggle source
# File lib/fifthed_sim/nodes/multi_node.rb, line 48 def reroll self.class.new(@array.map(&:reroll)) end
roll_count()
click to toggle source
How many dice did we roll?
# File lib/fifthed_sim/nodes/multi_node.rb, line 78 def roll_count @array.count end
value()
click to toggle source
Calculate the value of these dice
# File lib/fifthed_sim/nodes/multi_node.rb, line 72 def value @array.map(&:value).inject(:+) end
value_equation(terminal: false)
click to toggle source
# File lib/fifthed_sim/nodes/multi_node.rb, line 111 def value_equation(terminal: false) "(" + @array.map do |a| a.value_equation(terminal: terminal) end.join(", ") + ")" end
Private Instance Methods
occurences(num)
click to toggle source
# File lib/fifthed_sim/nodes/multi_node.rb, line 125 def occurences(num) dice_type = @array.first.type num_dice = @array.size 0.upto((num - num_dice) / dice_type).map do |k| ((-1) ** k) * combination(num_dice, k) * combination(num - (dice_type*k) - 1, num_dice- 1) end.inject(:+) end