[Tutor] generating independent random numbers

Dave Angel davea at ieee.org
Wed Sep 29 19:53:28 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 again.  Put your comments after the part you're quoting, 
not before.

You still have a problem in the code, and I still think it's in the 
2*self.c, though I don't have time to debug it.

Look up the day for  1/1/2099, and for 1/1/2100  and it comes out the 
same.  That's not correct.  No adjacent years start on the same day, 
it's always either one day or two.

You have too much in one function (method), which makes it hard to debug 
it.  Factor it into separate functions, and then test each 
independently.  And using k  for day and d for year make no sense to me, 
though perhaps it does in some other language.

DaveA





More information about the Tutor mailing list