Created
June 27, 2024 02:33
-
-
Save clagreca03/41e9d0e8a8895d369a8fbc4269b583f2 to your computer and use it in GitHub Desktop.
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 calendar | |
from random import randint | |
from datetime import date | |
year = 2023 | |
num = 1 | |
def generate_random_date(year: int, month: int) -> date: | |
""" Return a random date in a given year and month """ | |
days_in_month = calendar.monthrange(year, month)[1] | |
random_day = randint(1, days_in_month) | |
return date(year, month, random_day) | |
def generate_random_days_each_month(year: int, n: int = 1) -> list[date]: | |
""" Return a list of 'n' random dates for every month in the given year """ | |
r_dates = [generate_random_date(year, i) for i in range(1, 13) for _ in range(n) ] | |
return r_dates | |
if __name__ == "__main__": | |
r_dates = generate_random_days_each_month(year, num) | |
print(r_dates) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment