Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate random times from (9am – 5pm)

I need to randomly generate 150 samples of time in (hh:mm:ss).
Generated time must be within the range of (9am – 5pm)
How do I set the conditions for the time range?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Python 3.X:

import random

def getRandTime():
    hh = random.randrange(9,17)
    mm = random.randrange(60)
    ss = random.randrange(60)
    return f'{hh:>02}:{mm:>02}:{ss:>02}'
    
for i in range(150):
    print(getRandTime())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading