-
-
Save ccooke/6688909 to your computer and use it in GitHub Desktop.
An incomplete probability function, with a manual example of some prospective code to work on it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProbabilityDensity < Array | |
def probability | |
Rational(1,count) | |
end | |
def to_density | |
if block_given? | |
each do |v| | |
if v.respond_to? :probability | |
v.to_density do |(subv,subp)| | |
yield [ subv, probability * subp ] | |
end | |
else | |
yield [ v, probability ] | |
end | |
end | |
else | |
Enumerator.new(self, :to_density) | |
end | |
end | |
def []=(index, value) | |
super(index, ProbabilityDensity.new([value])) | |
end | |
def <<(value) | |
super(ProbabilityDensity.new([value])) | |
end | |
def to_s | |
to_density.to_a.to_s | |
end | |
alias_method :inspect, :to_s | |
end | |
# Imagine a dice expression: 1d4!p r4 | |
# first we set up the basic die roll: | |
v = ProbabilityDensity.new | |
v << 1 | |
v << 2 | |
v << 3 | |
v << 4 | |
# Now we handle the !p. Code will replace the 4, because it would be rolled | |
# (the example below is a simulation of what the code will do) | |
v[3] << 5 | |
v[3] << 6 | |
v[3] << 7 | |
v | |
# This will iterate up to the cap for !p. Let's assume the cap is 2 | |
v[3][3] << 8 | |
v[3][3] << 9 | |
v[3][3] << 10 | |
v | |
# Now we pass it to the reroll function, which iterates over each value. | |
# When it finds the 4, it deletes it | |
v[3].delete(4) | |
# And now we have: | |
v | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
probability.rb(main):037:0* # Imagine a dice expression: 1d4!p r4 | |
probability.rb(main):038:0* | |
probability.rb(main):039:0* # first we set up the basic die roll: | |
probability.rb(main):040:0* v = ProbabilityDensity.new | |
=> [] | |
probability.rb(main):041:0> v << 1 | |
=> [[1, (1/1)]] | |
probability.rb(main):042:0> v << 2 | |
=> [[1, (1/2)], [2, (1/2)]] | |
probability.rb(main):043:0> v << 3 | |
=> [[1, (1/3)], [2, (1/3)], [3, (1/3)]] | |
probability.rb(main):044:0> v << 4 | |
=> [[1, (1/4)], [2, (1/4)], [3, (1/4)], [4, (1/4)]] | |
probability.rb(main):045:0> | |
probability.rb(main):046:0* # Now we handle the !p. Code will replace the 4, because it would be rolled | |
probability.rb(main):047:0* # (the example below is a simulation of what the code will do) | |
probability.rb(main):048:0* v[3] << 5 | |
=> [[4, (1/2)], [5, (1/2)]] | |
probability.rb(main):049:0> v[3] << 6 | |
=> [[4, (1/3)], [5, (1/3)], [6, (1/3)]] | |
probability.rb(main):050:0> v[3] << 7 | |
=> [[4, (1/4)], [5, (1/4)], [6, (1/4)], [7, (1/4)]] | |
probability.rb(main):051:0> v | |
=> [[1, (1/4)], [2, (1/4)], [3, (1/4)], [4, (1/16)], [5, (1/16)], [6, (1/16)], [7, (1/16)]] | |
probability.rb(main):052:0> # This will iterate up to the cap for !p. Let's assume the cap is 2 | |
probability.rb(main):053:0* v[3][3] << 8 | |
=> [[7, (1/2)], [8, (1/2)]] | |
probability.rb(main):054:0> v[3][3] << 9 | |
=> [[7, (1/3)], [8, (1/3)], [9, (1/3)]] | |
probability.rb(main):055:0> v[3][3] << 10 | |
=> [[7, (1/4)], [8, (1/4)], [9, (1/4)], [10, (1/4)]] | |
probability.rb(main):056:0> v | |
=> [[1, (1/4)], [2, (1/4)], [3, (1/4)], [4, (1/16)], [5, (1/16)], [6, (1/16)], [7, (1/64)], [8, (1/64)], [9, (1/64)], [10, (1/64)]] | |
probability.rb(main):057:0> | |
probability.rb(main):058:0* # Now we pass it to the reroll function, which iterates over each value. | |
probability.rb(main):059:0* # When it finds the 4, it deletes it | |
probability.rb(main):060:0* v[3].delete(4) | |
=> 4 | |
probability.rb(main):061:0> | |
probability.rb(main):062:0* # And now we have: | |
probability.rb(main):063:0* v | |
=> [[1, (1/4)], [2, (1/4)], [3, (1/4)], [5, (1/12)], [6, (1/12)], [7, (1/48)], [8, (1/48)], [9, (1/48)], [10, (1/48)]] | |
probability.rb(main):064:0> | |
probability.rb(main):065:0* | |
probability.rb(main):065:0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment