Skip to content

Instantly share code, notes, and snippets.

@r14c
Last active July 6, 2016 01:18
Show Gist options
  • Save r14c/e487ed22b57984c61d14 to your computer and use it in GitHub Desktop.
Save r14c/e487ed22b57984c61d14 to your computer and use it in GitHub Desktop.
Coinflip!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Takes an arbitrary number of command line arguments and randomly selects one
to return. The amount of entropy involved can be adjusted by setting the
environment variable ```RANDBYTES```, which defaults to 4096.
"""
from __future__ import print_function
import os
import sys
import random
RANDBYTES = int(os.environ.get('RANDBYTES', 4096))
def coinflip(*args):
random.seed(os.urandom(RANDBYTES))
return random.choice(list(args))
if __name__ == '__main__':
try:
print(coinflip(*sys.argv[1:]))
except IndexError:
print("coinflip: usage: coinflip OPTIONS...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment