Last active
July 2, 2023 01:05
-
-
Save yysaki/027a07f1e629c9b7c08bc72dac4a1cca to your computer and use it in GitHub Desktop.
遊戯王 1枚初動率
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
for i in $(seq 3 12); do ruby yugioh_probability.rb 40 5 $i; done | |
# deck: 40, hand: 5, hit: 3 -> 33.76% | |
# deck: 40, hand: 5, hit: 4 -> 42.71% | |
# deck: 40, hand: 5, hit: 5 -> 50.66% | |
# deck: 40, hand: 5, hit: 6 -> 57.71% | |
# deck: 40, hand: 5, hit: 7 -> 63.93% | |
# deck: 40, hand: 5, hit: 8 -> 69.40% | |
# deck: 40, hand: 5, hit: 9 -> 74.18% | |
# deck: 40, hand: 5, hit: 10 -> 78.34% | |
# deck: 40, hand: 5, hit: 11 -> 81.95% | |
# deck: 40, hand: 5, hit: 12 -> 85.06% |
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
# frozen_string_literal: true | |
def factorial(number) = Rational((1..number).reduce(1, :*)) | |
def permutation(cards, hand) = factorial(cards) / factorial(cards - hand) | |
def probability(deck, hand, hit) = 1 - (permutation(deck - hit, hand) / permutation(deck, hand)) | |
def main(deck:, hand:, hit:) | |
percent = "#{format('%.2f', probability(deck, hand, hit) * 100)}%" | |
puts "deck: #{deck}, hand: #{hand}, hit: #{hit} -> #{percent}" | |
end | |
deck = (ARGV[0] || 40).to_i | |
hand = (ARGV[1] || 5).to_i | |
hit = (ARGV[2] || 7).to_i | |
main(deck:, hand:, hit:) if __FILE__ == $PROGRAM_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment