Home > OS >  Generate random times from (9am - 5pm)
Generate random times from (9am - 5pm)

Time:02-22

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?

CodePudding user response:

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())
  • Related