Dear! Tell me, please, how will it be correct to register getting a random value from a selectable period of time? Suppose there is a date range 01/01/2017 - 01/01/2018, if you select any date, a random value will be output from the interval (random.randint (0, 100))
- oneRelated issue - MaxU
- How is the random value and time span related? Or not, and I mean just some action when choosing a date in the range? - MBo
- @MBo, Hello, ideally for the program to work like this: Enter the date (the date is entered) and after entering the program returns a random value (from the specified range). - Deefrost
|
2 answers
Looks like you want something like this (enter 12/12/2012):
import random from datetime import datetime d1 = datetime.strptime("01/01/2001","%d/%m/%Y") d3 = datetime.strptime("01/01/2021","%d/%m/%Y") d2 = datetime.strptime(input(),"%d/%m/%Y") if (d1 <= d2 <= d3): print(random.randint(0, 100)) |
import random from datetime import datetime as DT from datetime import timedelta def get_random_date(start, end): delta = end - start return start + timedelta(random.randint(0, delta.days)) start_dt = DT.strptime('01.01.2017', '%d.%m.%Y') end_dt = DT.strptime('01.01.2018', '%d.%m.%Y') for _ in range(10): print(get_random_date(start_dt, end_dt)) result:
2017-03-30 00:00:00 2017-08-29 00:00:00 2017-01-13 00:00:00 2017-03-25 00:00:00 2017-10-05 00:00:00 2017-02-13 00:00:00 2017-05-24 00:00:00 2017-11-03 00:00:00 2017-02-14 00:00:00 2017-05-13 00:00:00 |