<commit_before>import random
from plugin import CommandPlugin, PluginException


class CoinFlip(CommandPlugin):
    """
    Flip a coin
    """
    max_coin_flips = 1000000

    def __init__(self):
        CommandPlugin.__init__(self)
        self.triggers = ['coin', 'coinflip']
        self.short_help = 'Flip a coin'
        self.help = 'Flip a coin or number of coins'
        self.help_example = ['!coin', '!coinflip 5']

    def on_command(self, bot, event, response):
        args = event['text']

        if not args:
            response['text'] = 'A coin is flipped and it is *_%s_*!' % random.choice(['Heads', 'Tails'])
        else:
            try:
                tosses = int(args)
                if tosses <= 0:
                    raise PluginException('Invalid argument! No coins to flip!')

                # Avoid taking too long to generate coin flips
                if tosses > CoinFlip.max_coin_flips:
                    raise PluginException(
                        'Invalid argument! Number of coins to flip is too large! Max flips is `%s`.' % CoinFlip.max_coin_flips)

                rand_bits = bin(random.getrandbits(tosses))[2:]
                heads = rand_bits.count('0')
                tails = rand_bits.count('1')
                response['text'] = '*_%s_* coins are flipped and the result is *_%s Heads_* and *_%s Tails_*!' % (<commit_msg>Fix coin flip math<commit_after>import random
from plugin import CommandPlugin, PluginException


class CoinFlip(CommandPlugin):
    """
    Flip a coin
    """
    max_coin_flips = 1000000

    def __init__(self):
        CommandPlugin.__init__(self)
        self.triggers = ['coin', 'coinflip']
        self.short_help = 'Flip a coin'
        self.help = 'Flip a coin or number of coins'
        self.help_example = ['!coin', '!coinflip 5']

    def on_command(self, bot, event, response):
        args = event['text']

        if not args:
            response['text'] = 'A coin is flipped and it is *_%s_*!' % random.choice(['Heads', 'Tails'])
        else:
            try:
                tosses = int(args)
                if tosses <= 0:
                    raise PluginException('Invalid argument! No coins to flip!')

                # Avoid taking too long to generate coin flips
                if tosses > CoinFlip.max_coin_flips:
                    raise PluginException(
                        'Invalid argument! Number of coins to flip is too large! Max flips is `%s`.' % CoinFlip.max_coin_flips)

                rand_bits = bin(random.getrandbits(tosses))[2:]
                heads = rand_bits.count('0')
                tails = tosses - heads
                response['text'] = '*_%s_* coins are flipped and the result is *_%s Heads_* and *_%s Tails_*!' % (
{'fraction_non_alphanumeric': 0.1088139281828074, 'fraction_numerical': 0.008342401160681901, 'mean_word_length': 1.9655913978494624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2928525', 'n_tokens_mistral': 848, 'n_tokens_neox': 764, 'n_words': 242}