Python random word generator I want to implement a random word generator that would not use Import statements. The solution is working for a *nix system. The solution does yield a random word, but please suggest:


How to make the function optimally/safely return words with a higher position in the list(e.g. now we're almost guaranteed to return an 'A' word)
How to get a word with min/max length instead of length
Should I break this into smaller functions?
How to make the code more readable/higher performance
Is there a better approach to this problem? How to tell which modules are better off imported instead of implemented from scratch(except for the built-ins)?


def generate_random_word(length=8):
    """
    Return a random string from /usr/share/dict/words on a *nix system
    """
    with open('/usr/share/dict/words', 'r') as words_file, open("/dev/random", 'rb') as random_file:
        random_words = words_file.read().split('\n')
        size_words = [x for x in random_words if len(x) == length]
        for _ in range(0, 100):
            while True:
                try:
                    random_word = size_words[(int.from_bytes(random_file.read(1), 'big'))]
                except IndexError:
                    continue
                break


    return random_word
I am surprised that you chose to use /dev/random  although I can understand that for some purposes like generating private keys a source of strong entropy is preferred and there are even devices for that. Then this discussion could interest you. But the built-in random module in python should be sufficient here.
To read the file to a list of items you can just do this:
with open('/usr/share/dict/words', 'r') as f:
    lines = f.read().splitlines()

Good thing is that you are using the context manager for the open function.
Try to avoid IndexError rather than handle it and ignore it.
If your file is small (check size before opening) you can be lazy, load all items to a list, then filter it, and return one item at random:
def generate_random_word(min_length=8, max_length=13):
    with open('/usr/share/dict/words', 'r') as f:
        lines = f.read().splitlines()

    # select words matching desired length
    # selection = [line for line in lines if len(line) &lt;= max_length and len(line) &gt;= min_length ]
    selection = [line for line in lines if min_length &lt;= len(line) &lt;= max_length]
    # no match found
    if len(selection) == 0:
        return None

    return random.choice(selection)

If no matching item is found (or the file is empty) then I chose to return None.
If you want to filter lines at the source the implementation could be like this:
def generate_random_word(min_length=8, max_length=13):
    with open('/usr/share/dict/words', 'r') as f:
        selection = [line for line in f.read().splitlines() if min_length &lt;= len(line) &lt;= max_length]

    # no match found
    if len(selection) == 0:
        return None

    return random.choice(selection)

The file has to exist but it can be empty, then there is no error but the function will return None. Use os.exists to test that the file is present.
Yes, there is an import but it is a built-in module, does not require installation of a third-party module with PIP. It is also portable and not just Unix.
However, if you insist on /dev/random and want no import one thing you could do is retrieve a random integer like what you are doing now and use it in a modulo-type fashion against the list of matching items, to pick one word at random. Be careful with implementation as you may introduce unwanted bias in the selection. Random functions exist for a reason.
While it is possible to rely solely on /dev/random reimplementing the functionality with decent randomization will result in more code and reinventing the wheel.