Why does this choke?

S Kemplay skemplay at dingley.net
Sun Nov 9 00:23:06 EST 2003


Hi Dennis,

Thanks for your advice,

I really like the way you handled the dates and can see your script generates 
fewer random numbers for each date generated.  

It still has to check whether the dates in the list (for unique dates). I 
guess breaking a big list down into shorter lists would help?

Sean Kemplay



On Saturday 08 November 2003 18:33, Dennis Lee Bieber wrote:
> Dennis Lee Bieber fed this fish to the penguins on Friday 07 November
> 2003 08:55 am:
>
>         Obviously I wasn't full awake when I wrote that -- a few bugs were
> left... I think this one is more suited:
>
> import random
>
> #       start with MARCH to avoid the confusion of 365 vs 366 (leap)
> MONTHS = [ 'Mar', 'Apr', 'May',
>            'Jun', 'Jul', 'Aug',
>            'Sep', 'Oct', 'Nov',
>            'Dec', 'Jan', 'Feb' ]
>
> #       a list of the start day for each month
> LENGTH = [  1,      32,     62,
>             93,     123,    154,
>             185,    215,    246,
>             276,    307,    338 ]
>
> def RandomDate():
>         #       generate a random date index within a year
>         if random.randint(1, 4) == 4:
>                 dayindex = random.randint(1, 366)
>         else:
>                 dayindex = random.randint(1, 365)
>
>         monthindex = 0
>         while monthindex < 12:
>                 #       scan LENGTH to find which month
>                 if dayindex < LENGTH[monthindex]: break
>                 monthindex += 1
>
>         monthindex -= 1                 #       adjust for last loop
>
>         month = MONTHS[monthindex]
>         day = dayindex - LENGTH[monthindex] + 1
>
>         return (month, day)
>
>
> if __name__ == "__main__":
>         dates = []
>         while len(dates) < 450: #       if the limit is >365, do not use
> UNIQUE aDate = RandomDate()
>                 dates.append(aDate)     #       replace this with next two
> for UNIQUE #               if aDate not in dates:
> #                       dates.append(aDate)
>
>
>         print dates






More information about the Python-list mailing list