lundi 4 mai 2020

generate random dates with days and time in python

hey guys i was trying to create a code that generate dates in given range but i faced some problems the code is as follows:

import datetime
import random
import os
from random import randrange
year_b = 2019 
month_b = 1
day_b = 1
year_e = 2020 
month_e = 1 
day_e = 1 

 def date_range(start, end, step: datetime.timedelta):
     while start < end:
        yield start
        start += step


rand_list=[5,8,6,9,10]


for d in date_range(
    start=datetime.datetime(year_b, month_b, day_b),
    end=datetime.datetime(year_e, month_e, day_e),
    step=datetime.timedelta(days=randrange(10)),
    ):
    print(d)

os.system("pause")

output:

2019-01-01 00:00:00
2019-01-09 00:00:00
2019-01-17 00:00:00
2019-01-25 00:00:00
2019-02-02 00:00:00
Press any key to continue . . . 

first problem that the code only select one random value from the list and add it to the date but i need it to select random value for each date generated

second problem is that code the time it not been generated randomly

any ideas to solve those problems???!!




Aucun commentaire:

Enregistrer un commentaire