0

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.

3
  • 1
    Are you sure it didn't just return 0? Commented Jul 5, 2018 at 6:17
  • Actually, wait, why are you trying to add hours to a date? Commented Jul 5, 2018 at 6:18
  • I want to randomise some entries for scheduling purposes. Commented Jul 5, 2018 at 6:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense. Thanks for your response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.