[Tutor] generating independent random numbers
Carter Danforth
carter.danforth at gmail.com
Tue Sep 28 00:55:36 CEST 2010
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100927/c1f18a55/attachment.html>
More information about the Tutor
mailing list