In order to insert fake users into my database for test purposes (see the previous post), I forget to generate a random birth date but I’ve currently solved the problem using the two modules datetime and random:
from datetime import datetime import random year = random.choice(range(1950, 2001)) month = random.choice(range(1, 13)) day = random.choice(range(1, 29)) birth_date = datetime(year, month, day)
in practice I randomize the three parameters: year, month and day using choice(), which is a really cool method that returns a random element from a collection, collection that I dynamically created using range(). The code above create a random date bewteen January 1 1950 and December, 28 2000.
as Jason has written in the comments, there is no need to use
range(), since the random module provides a
randint() method (which returns a random integer), so the code can be simplified in this way:
from datetime import datetime import random year = random.randint(1950, 2000) month = random.randint(1, 12) day = random.randint(1, 28) birth_date = datetime(year, month, day)