Random Color Generator with Integer Inputs

  • Share this:

Code introduction


This function takes two integer arguments and returns a string of randomly chosen color names, with the number of colors not exceeding the minimum of the two arguments.


Technology Stack : Django, `django.utils.crypto.get_random_string`, `random`

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_color(arg1, arg2):
    from django.utils.crypto import get_random_string
    import random
    color_names = ['red', 'green', 'blue', 'yellow', 'purple', 'orange']
    if not isinstance(arg1, int) or not isinstance(arg2, int):
        return "Error: arguments must be integers"
    num_colors = min(arg1, arg2)
    colors = [random.choice(color_names) for _ in range(num_colors)]
    return ' '.join(colors)