[Tutor] generating independent random numbers

Peter Otten __peter__ at web.de
Wed Sep 29 11:42:29 CEST 2010


Carter Danforth wrote:

> Thanks for the replies, Dave and Joel. The reason I'm not just using the
> time or datetime modules for a random date is because it's restricted to
> 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer

The datetime module is not restricted to 1970...2038. It allows years 
1...9999 (it uses the Gregorian calendar even before its adoption). 

>>> import datetime
>>> datetime.MINYEAR, datetime.MAXYEAR
(1, 9999)
>>> datetime.date(1500, 2, 29)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day is out of range for month
>>> datetime.date(1600, 2, 29)
datetime.date(1600, 2, 29)

The range allowed by the time module is probably implementation dependent. I 
can do things like

>>> time.gmtime(-11670998400)
time.struct_time(tm_year=1600, tm_mon=2, tm_mday=29, tm_hour=0, tm_min=0, 
tm_sec=0, tm_wday=1, tm_yday=60, tm_isdst=0)

>>> time.gmtime(2**55)
time.struct_time(tm_year=1141709097, tm_mon=6, tm_mday=13, tm_hour=6, 
tm_min=26, tm_sec=8, tm_wday=6, tm_yday=164, tm_isdst=0)

>>> time.gmtime(2**56)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: (75, 'Value too large for defined data type')

Peter



More information about the Tutor mailing list