import random
import string
import time


def generate_random_filename(randlen=2, suffix=None):
    """
    Generates a random string with a specified prefix, useful for generating
    random filenames. No two filenames will be the same if executed on the same
    machine.

    *randlen* specifies the length of the filename, while *suffix* allows you to
    append a suffix at the end of the file name, i.e xxx.gif or xxx.html.
    Returns a string.
    """

    try:
        int(randlen)
        if randlen > 7:
            randlen = 7
    except:
        randlen = 2

    if suffix:
        return "%s%s.%s" % ( "".join(random.sample(string.digits + string.ascii_lowercase, randlen)), int(time.time()), suffix.strip() )
    else:
        return "%s%s" % ( "".join(random.sample(string.digits + string.ascii_lowercase, randlen)), int(time.time()) )