Last active
December 17, 2015 01:19
-
-
Save nsabine/5527337 to your computer and use it in GitHub Desktop.
Generators for random integers or words
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
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