Velocity Reviews > Why does this choke?

Why does this choke?

S Kemplay
Guest
Posts: n/a

 11-07-2003
Hi all,

I wrote a script to choose random dates for a statistics assignment.
I only need to choose 30 dates from one year with no leap years and it works
fine. However I tested with different numbers of dates. It hangs from 450 up.
I only need 30 dates but it would be good to know why it hangs. (My coding
probably has something to do with it )

import random

def getmonth():
month = random.randint(1,12)
return month

def getday(month, leaps):
thirtyones = [1,3,5,7,8,10,12]
thirties = [4,6,9,11]
if month in thirtyones:
day = random.randint(1,31)
elif month in thirties:
day = random.randint(1,30)
else:
if leaps == 1: leap = random.randint(1,4)
else: leap = 1
if leap in [2,3,4]:
day = random.randint(1,29)
else:
day = random.randint(1,2
return day

def getdates(n, leaps):
dates = []
i = 0
while i < n:
month = getmonth()
day = getday(month, leaps)
if (day, month) in dates:
continue
i += 1
dates.append((day, month))
return dates

Thanks

Sean Kemplay

John J. Lee
Guest
Posts: n/a

 11-07-2003
S Kemplay <(E-Mail Removed)> writes:

> I wrote a script to choose random dates for a statistics assignment.
> I only need to choose 30 dates from one year with no leap years and it works
> fine. However I tested with different numbers of dates. It hangs from 450 up.
> I only need 30 dates but it would be good to know why it hangs. (My coding
> probably has something to do with it )

[...]
> if (day, month) in dates:
> continue

[...]

This looks like the culprit. I bet it really hangs from 366 up <wink>.

John

S Kemplay
Guest
Posts: n/a

 11-07-2003
Thanks John, that explains it

Sean Kemplay

On Friday 07 November 2003 23:30, John J. Lee wrote:
> S Kemplay <(E-Mail Removed)> writes:
> > I wrote a script to choose random dates for a statistics assignment.
> > I only need to choose 30 dates from one year with no leap years and it
> > works fine. However I tested with different numbers of dates. It hangs
> > from 450 up. I only need 30 dates but it would be good to know why it
> > hangs. (My coding probably has something to do with it )

>
> [...]
>
> > if (day, month) in dates:
> > continue

>
> [...]
>
> This looks like the culprit. I bet it really hangs from 366 up <wink>.
>
>
> John