Aparna Kulkarni
Aparna Kulkarni

Reputation: 39

python random.randint() did not return any value

I need to append random hours to date field. For this I used following code:

datetime.utcnow().date() + relativedelta(hours=random.randint(0,23))

This returned response:

datetime.date(2018, 7, 5)

Above response is not reproducible. Wondering if using random.randint() is reliable. Please let me know what caused this to occur or what other solution I can use for this problem.

Upvotes: 0

Views: 132

Answers (1)

Lutz Büch
Lutz Büch

Reputation: 363

This happens exactly when random.randint(0,23) returns 0. In that case, you get a final result of type datetime.date instead of datetime.datetime, because the delta is basically zero. So random.randint() is reliable, but sometimes it does return 0, as expected.

Upvotes: 1

Related Questions