Re: [Tutor] generating independent random numbers

2010-09-30 Thread Steven D'Aprano
On Thu, 30 Sep 2010 02:38:31 pm Dave Angel wrote:

[snip nearly 300 lines of quoted text and 7 lines of new content]

Dave, and Carter, are the delete and backspace keys on your keyboards 
broken?

There's no need to quote the ENTIRE thread every time you reply, and 
then quote it in full AGAIN a handful of minutes later.

Please trim your quoting to only show the relevant parts of the email 
that you are actually replying to, or to show context. Nobody wants to 
have to scroll past pages upon pages of quoted text that we've already 
read four or five times to get to your reply.


Thank you.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-29 Thread Dave Angel



On 2:59 PM, Dave Angel wrote:

 On 9/29/2010 9:17 PM, Carter Danforth wrote:

On Wed, Sep 29, 2010 at 1:53 PM, Dave Angel  wrote:


  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



  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


Hey Dave, you probably left c and y alone when comparing the years. 
If the
date's 1/1/2099, then self.c = 20 and self.y=99. If you try doing it 
again
while changing those values, for 1/1/2099, the day comes out to be 
Thursday,

and for 1/1/2100 you'll get Wednesday.

Glad you pointed out the 2100 date though, there actually was a 
problem in
it, but it's not the 2*self.c; I had to account for d = y - 1 when y 
= 00

(zeller subtracts months by 2, so it needs to be the previous yr for
jan/feb).

Below is the updated code, I put in a few comments to make it read 
easier.


--

import random, time, datetime, calendar

class Date:
 def __init__(self):
 self.c = random.randint(16,30) # first two digits in a year
 self.y = random.randint(0,99)  # last two digits in a year
 self.month = random.randint(1,12)
 self.year = self.c*100 + self.y

 apr = [4,6,9,11]
 feb = [2]
 notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 
2700,

2900, 3000]

 if self.month in feb:  # assigns days, given the month
 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]:# months in zeller's rule are
subtracted by 2
 if self.y == 0:# ne

Re: [Tutor] generating independent random numbers

2010-09-29 Thread Dave Angel

 On 9/29/2010 9:17 PM, Carter Danforth wrote:

On Wed, Sep 29, 2010 at 1:53 PM, Dave Angel  wrote:


  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



  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



Hey Dave, you probably left c and y alone when comparing the years. If the
date's 1/1/2099, then self.c = 20 and self.y=99. If you try doing it again
while changing those values, for 1/1/2099, the day comes out to be Thursday,
and for 1/1/2100 you'll get Wednesday.

Glad you pointed out the 2100 date though, there actually was a problem in
it, but it's not the 2*self.c; I had to account for d = y - 1 when y = 00
(zeller subtracts months by 2, so it needs to be the previous yr for
jan/feb).

Below is the updated code, I put in a few comments to make it read easier.

--

import random, time, datetime, calendar

class Date:
 def __init__(self):
 self.c = random.randint(16,30) # first two digits in a year
 self.y = random.randint(0,99)  # last two digits in a year
 self.month = random.randint(1,12)
 self.year = self.c*100 + self.y

 apr = [4,6,9,11]
 feb = [2]
 notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700,
2900, 3000]

 if self.month in feb:  # assigns days, given the month
 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]:# months in zeller's rule are
subtracted by 2
 if self.y == 0:# need to acct for jan/feb year
change
 d = 99
   

Re: [Tutor] generating independent random numbers

2010-09-29 Thread Carter Danforth
On Wed, Sep 29, 2010 at 1:53 PM, Dave Angel  wrote:

>  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
>>
>> 
>>
>>  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
>
>
Hey Dave, you probably left c and y alone when comparing the years. If the
date's 1/1/2099, then self.c = 20 and self.y=99. If you try doing it again
while changing those values, for 1/1/2099, the day comes out to be Thursday,
and for 1/1/2100 you'll get Wednesday.

Glad you pointed out the 2100 date though, there actually was a problem in
it, but it's not the 2*self.c; I had to account for d = y - 1 when y = 00
(zeller subtracts months by 2, so it needs to be the previous yr for
jan/feb).

Below is the updated code, I put in a few comments to make it read easier.

--

import random, time, datetime, calendar

class Date:
def __init__(self):
self.c = random.randint(16,30) # first two digits in a year
self.y = random.randint(0,99)  # last two digits in a year
self.month = random.randint(1,12)
self.year = self.c*100 + self.y

apr = [4,6,9,11]
feb = [2]
notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700,
2900, 3000]

if self.month in feb:  # assigns days, given the month
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 

Re: [Tutor] generating independent random numbers

2010-09-29 Thread Dave Angel

 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



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



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-29 Thread Carter Danforth
Wow... I'm really slipping here with the leaps years, good catch on the
2000s. And yeah, a list does make a whole lot more sense. Thanks Dave.

I've checked multiple sources on Zeller's formula, initially came across it
on this book on vedic math (highly recommend it): http://amzn.to/bNXBM6. But
here's the Wikipedia on it:
http://en.wikipedia.org/wiki/Zeller%27s_congruence

It's not *2 in the Julian calendar, but it is in Gregorian, which is what
we're also using for the leap yrs - http://en.wikipedia.org/wiki/Leap_year


On Tue, Sep 28, 2010 at 9:34 PM, Dave Angel  wrote:

>  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
>>
>>
>> 
>>
>>  (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
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-29 Thread Peter Otten
Ewald Ertl wrote:

>> Just an attempt from my side:
> The year 1500 didn't have a 29th of February, the 28th work for me but
> 29th also fails here.
 datetime.date( 1500, 2, 28 )
> datetime.date(1500, 2, 28)
 datetime.date( 1500, 2, 29 )
> Traceback (most recent call last):
> File "", line 1, in 
> ValueError: day is out of range for month

Yes, it's an expected "failure". I should have said that explicitly.

Peter 


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-29 Thread Ewald Ertl
Hi,

On Wed, Sep 29, 2010 at 11:42 AM, Peter Otten <__pete...@web.de> wrote:

> 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... (it uses the Gregorian calendar even before its adoption).
>
> >>> import datetime
> >>> datetime.MINYEAR, datetime.MAXYEAR
> (1, )
> >>> datetime.date(1500, 2, 29)
> Traceback (most recent call last):
>  File "", line 1, in 
> ValueError: day is out of range for month
> >>> datetime.date(1600, 2, 29)
> datetime.date(1600, 2, 29)
>
> Just an attempt from my side:
The year 1500 didn't have a 29th of February, the 28th work for me but 29th
also fails here.
>>> datetime.date( 1500, 2, 28 )
datetime.date(1500, 2, 28)
>>> datetime.date( 1500, 2, 29 )
Traceback (most recent call last):
  File "", line 1, in 
ValueError: day is out of range for month


HTH
Ewald
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-29 Thread Peter Otten
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... (it uses the Gregorian calendar even before its adoption). 

>>> import datetime
>>> datetime.MINYEAR, datetime.MAXYEAR
(1, )
>>> datetime.date(1500, 2, 29)
Traceback (most recent call last):
  File "", line 1, in 
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 "", line 1, in 
ValueError: (75, 'Value too large for defined data type')

Peter

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-28 Thread Dave Angel

 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





(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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-28 Thread Carter Danforth
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


On Mon, Sep 27, 2010 at 10:21 PM, Dave Angel  wrote:

>
>
> On 2:59 PM, Steven D'Aprano wrote:
>
> On Tue, 28 Sep 2010 08:55:36 am Carter Danforth wrote:
>
>
>  class Date:
> c = random.randint(16,30)
> y = random.randint(0,99)
> month = random.randint(1,12)
>
>  Here's your problem: you are creating a class where all the attributes
> (called "members" in some other languages) belong to the class and are
> shared by all instances.
>
> Python classes are themselves objects, and the code inside the class
> body gets executed *once*, when the class is created. So in this case,
> the Date class chooses a single random month, *once*, and all instances
> share this attribute Date.month.
>
> To get the behaviour you are after, you need to use instance attributes,
> which means referring to self. The usual place to do this is in the
> __init__ method, which is called when the instance is being
> initialised:
>
> class Date:
> def __init__(self):
> self.month = random.randint(1,12)
> # etc.
>
>
>
> By the way, why do you calculate a century and year separately, then add
> c+y to get the year? It would be easier to just say:
>
> year = random.randint(1600, 3099)
>
>
>
>
>  That's the big problem, although it's also worth pointing out that you'll
> need a new instance each time through the loop.  It's not enough to call
> Date(), you also have to bind it to a name, and use that name for attribute
> lookup.So something like
> mydate = Date()
> year = mydate.y + 
>
> But there are at least a few subtle problems left.  One is that many of the
> years are divisible by four but do not have 29 days in February.  For
> example, 1800, 1900, 2100 are not leap years.
>
> Next problem is that the dates are not evenly distributed over the entire
> range of years.  The 14th of February will be more likely to be chosen than
> the sixth of July.  You can decide that this is deliberate, but it is a
> consideration.
>
> Third, the program doesn't do anything to check the user's answer.  For
> that matter, there's no timing going on either.
>
> Depending on the learning goals of this project, I'd consider using the
> datetime module, and method:
>
> mydate = date.fromordinal(*ordinal*)
>
> Now you can make a single randint() call, once you precalculate the
> starting and ending dates desired.   And this module also gives you other
> things you need, such as the weekday() method.
>
> DaveA
>
>
___
Tutor maillist  -  Tutor@pytho

Re: [Tutor] generating independent random numbers

2010-09-27 Thread Dave Angel



On 2:59 PM, Steven D'Aprano wrote:

On Tue, 28 Sep 2010 08:55:36 am Carter Danforth wrote:


class Date:
 c = random.randint(16,30)
 y = random.randint(0,99)
 month = random.randint(1,12)

Here's your problem: you are creating a class where all the attributes
(called "members" in some other languages) belong to the class and are
shared by all instances.

Python classes are themselves objects, and the code inside the class
body gets executed *once*, when the class is created. So in this case,
the Date class chooses a single random month, *once*, and all instances
share this attribute Date.month.

To get the behaviour you are after, you need to use instance attributes,
which means referring to self. The usual place to do this is in the
__init__ method, which is called when the instance is being
initialised:

class Date:
 def __init__(self):
 self.month = random.randint(1,12)
 # etc.



By the way, why do you calculate a century and year separately, then add
c+y to get the year? It would be easier to just say:

year = random.randint(1600, 3099)



That's the big problem, although it's also worth pointing out that 
you'll need a new instance each time through the loop.  It's not enough 
to call Date(), you also have to bind it to a name, and use that name 
for attribute lookup.So something like

mydate = Date()
year = mydate.y + 

But there are at least a few subtle problems left.  One is that many of 
the years are divisible by four but do not have 29 days in February.  
For example, 1800, 1900, 2100 are not leap years.


Next problem is that the dates are not evenly distributed over the 
entire range of years.  The 14th of February will be more likely to be 
chosen than the sixth of July.  You can decide that this is deliberate, 
but it is a consideration.


Third, the program doesn't do anything to check the user's answer.  For 
that matter, there's no timing going on either.


Depending on the learning goals of this project, I'd consider using the 
datetime module, and method:


mydate = date.fromordinal(/ordinal/)

Now you can make a single randint() call, once you precalculate the 
starting and ending dates desired.   And this module also gives you 
other things you need, such as the weekday() method.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-27 Thread Joel Levine
You might try fixing up the following.  I did not match the scale to what you  
are looking for.  But the trick is to take advantage of the time module's association  
of numbers with dates:


import time
import random

""" Sample output

Wed Apr 29 14:35:58 1992
Thu Jun 24 12:04:15 1971
Fri Oct  7 01:29:28 1994
Wed Mar 23 10:33:14 1994
Sun Apr 12 12:17:56 1998
Wed May 12 06:41:33 1971
Mon Jun 15 09:15:31 1998
Fri Sep 14 18:26:22 1979
Fri Apr 28 00:55:57 1972
Fri Mar 26 00:43:12 2010
"""

def re_scale():


now=float(time.time())
early=100.
rang=(now-early)
return time.ctime((random.random())*rang+early)



for i in range(10):
print re_scale()

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] generating independent random numbers

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 08:55:36 am Carter Danforth wrote:

> class Date:
> c = random.randint(16,30)
> y = random.randint(0,99)
> month = random.randint(1,12)

Here's your problem: you are creating a class where all the attributes 
(called "members" in some other languages) belong to the class and are 
shared by all instances.

Python classes are themselves objects, and the code inside the class 
body gets executed *once*, when the class is created. So in this case, 
the Date class chooses a single random month, *once*, and all instances 
share this attribute Date.month.

To get the behaviour you are after, you need to use instance attributes, 
which means referring to self. The usual place to do this is in the 
__init__ method, which is called when the instance is being 
initialised:

class Date:
def __init__(self):
self.month = random.randint(1,12)
# etc.



By the way, why do you calculate a century and year separately, then add 
c+y to get the year? It would be easier to just say:

year = random.randint(1600, 3099)



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] generating independent random numbers

2010-09-27 Thread Carter Danforth
Hi, I'm writing a program that's testing speed calculation of calendar dates
from any date spanning 1600-3000. I want it to generate a random date and
then prompt the user to indicate the correct day of the week using Zeller's
formula.

Included below is just some of the code to show what I'm having trouble
with. The variables that need to be randomly called each time (in this case,
5 times) are c, y, month, and k (I use these inputs to get the value and
assign the date with Zeller's formula, not included below).

Problem I'm having is that on each while loop, those variables stay
constant. I'm not sure how to make the class independently random. How can I
change this with random so that on each while loop c, y, month, and k also
have different values?

Thanks

---

import random, calendar

class Date:
c = random.randint(16,30)
y = random.randint(0,99)
month = random.randint(1,12)

apr = [4,6,9,11]
feb = [2]

if month in feb:
if y%4 == 0:
k = random.randint(1,29)
else:
k = random.randint(1,28)
elif month in apr:
k = random.randint(1,30)
else:
k = random.randint(1,31)

n = 0
while n < 5:
Date()
year = Date.c*100 + Date.y

print '\n',calendar.month_name[Date.month], Date.k,',', year,'=',
answer = raw_input()
n+=1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor