Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Dice
- attr_reader :die, :maxSum, :minSum
- def initialize(*args)
- @die = args.dup.freeze
- @values = Array.new(@die.size, 1)
- @maxSum = @die.inject(:+)
- @minSum = @die.size
- end
- def roll
- @die.map { |d| rand(d) + 1 }
- end
- def roll_sum(num = 0)
- t = roll.sort
- if (num > 0) then
- t.slice!(0, num)
- elsif (num < 0) then
- t.slice!(num, num.abs)
- end
- t.inject { |sum, x| sum + x }
- end
- def roll_assoc
- @die.map { |d| [rand(d) + 1, d] }
- end
- def roll_sorted
- roll_assoc.sort do |a, b|
- if (a[0] == b[0]) then
- a[1] <=> b[1]
- else
- a[0] <=> b[0]
- end
- end
- end
- def incr
- i = 0
- while (i < @die.size) do
- @values[i] += 1
- if (@values[i] > @die[i]) then
- @values[i] = 1
- i += 1
- else
- break
- end
- end
- end
- def sum
- @values.inject(:+)
- end
- def min?
- sum == @minSum
- end
- def max?
- sum == @maxSum
- end
- def max
- @values.max
- end
- def min
- @values.min
- end
- def inspect
- @values
- end
- end
- myDice = Dice.new(20, 20)
- maxRolls = Hash.new(0)
- minRolls = Hash.new(0)
- totalRolls = 0
- loop do
- maxRolls[myDice.max] += 1
- minRolls[myDice.min] += 1
- totalRolls += 1
- myDice.incr
- break if (myDice.min?)
- end
- puts "#{totalRolls} total rolls"
- difficulty = 17
- difficultyTotal = 0
- maxRolls.each_pair do |roll, total|
- printf("%2d: %4d (%.2f%%)\n", roll, total, total.fdiv(totalRolls) * 100)
- difficultyTotal += total if roll >= difficulty
- end
- printf("%.2f%% success vs (%d)\n", difficultyTotal.fdiv(totalRolls), difficulty)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement