[Tutor] generating independent random numbers

Dave Angel davea at ieee.org
Wed Sep 29 03:34:52 CEST 2010


  On 9/28/2010 5:11 PM, 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
> about the leap years, Dave, as well the class instances; just updated it and
> it's all working now, and also included the rest of the code too w/ answer
> verification and time tracking.
>
> I want to start using this program to test myself for speed calculation
> using Zeller's formula, it's pretty cool for determining the days of dates -
> http://mathforum.org/dr/math/faq/faq.calendar.html
>
> Because of the way variables C and D are split up from the year in the
> formula, I split up the year for self.c and self.y.
>
> ------------------------
>
> import random, time, datetime, calendar
>
> class Date:
>      def __init__(self):
>          self.c = random.randint(16,30)
>          self.y = random.randint(0,99)
>          self.month = random.randint(1,12)
>          self.year = self.c*100 + self.y
>
>          apr = [4,6,9,11]
>          feb = [2]
>          notleap = [1700, 1800, 1900, 3000]
>
>          if self.month in feb:
>              if self.year%4 == 0:
>                  if self.year in notleap:
>                      self.k = random.randint(1,28)
>                  else:
>                      self.k = random.randint(1,29)
>              else:
>                  self.k = random.randint(1,28)
>          elif self.month in apr:
>              self.k = random.randint(1,30)
>          else:
>              self.k = random.randint(1,31)
>
>          if self.month in [1,2]:
>              d = self.y - 1
>              m = self.month + 10
>          else:
>              d = self.y
>              m = self.month - 2
>
>          z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c
>
>          if z<  0:
>              r = (abs(z)/7)*7 + z + 7
>          else:
>              r = z%7
>
>          dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4:
> 'Thursday', 5: 'Friday', 6: 'Saturday' }
>          self.day = dict[r]
>
> t1m = time.localtime().tm_min
> t1s = time.localtime().tm_sec
> t1 = t1m + t1s/100.0
> n = 0
> x = 0
>
> while n<  10:
>      newdate = Date()
>
>      print '\n',calendar.month_name[newdate.month], newdate.k,',',
> newdate.year,'=',
>      answer = raw_input()
>      if answer.capitalize() == newdate.day:
>          pass
>      else:
>          x += 1
>      n += 1
>
> t2m = time.localtime().tm_min
> t2s = time.localtime().tm_sec
> t2 = t2m + t2s/100.0
> td = t2 - t1
>
> print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal time:',td
>
>
> <snip>
>
(You top-posted your response, so your message is out of order)

I haven't tried to run your code, but there is at least one problem.

Your notleap list is very incomplete.

notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700, 2900, 3000]

I'm a little suspicious of your version of Zeller.  I wouldn't think 
that last term should have a 2* in it.

I'm not sure why you use a dictionary to calculate self.day.  A list 
would work just as well.

DaveA




More information about the Tutor mailing list