import random, time

WIDTH = 80
DENSITY_CHANGE = 0.02
DELAY = 0.1
STAR_CHAR = '*'
BLANK_CHAR = ' '


def print_row_of_stars(density):
    for i in range(WIDTH):
        if random.random() < density:
            print(STAR_CHAR, end='')
        else:
            print(BLANK_CHAR, end='')
    print()  # Print newline at the end of the row


current_density = 0.0
while True:
    # Keep increasing the density until it reaches 1.0:
    while current_density < 1.0:
        print_row_of_stars(current_density)
        time.sleep(DELAY)
        # Increase the star density:
        current_density = current_density + DENSITY_CHANGE

    # Keep decreasing the density until it reaches 0.0:
    while current_density > 0.0:
        print_row_of_stars(current_density)
        time.sleep(DELAY)
        # Decrease the star density:
        current_density = current_density - DENSITY_CHANGE
