Skip to content

Instantly share code, notes, and snippets.

@nsabine
Last active December 17, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsabine/5527337 to your computer and use it in GitHub Desktop.
Save nsabine/5527337 to your computer and use it in GitHub Desktop.
Generators for random integers or words
import random
def random_int(num: int = 1, min: int = 1, max: int = 100) -> [int]:
"""Return a list of random integers."""
ints = []
for i in xrange(num):
ints.append(random.randint(min,max))
return ints
def random_word(num: int = 1) -> [str]:
"""Return a list of random words."""
with open('/usr/share/dict/words', 'rt') as f:
words = f.readlines()
allwords = [ w.rstrip() for w in words ]
words = []
for w in random.sample(allwords, num):
words.append(w)
return words
if __name__ == '__main__':
print("Random Words: " + ','.join(random_word(5)))
print("Random Integers: " + ','.join([str(i) for i in random_int(5)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment