[Tutor] random number equations . . .
Glen Wheeler
gew75 at hotmail.com
Thu Jun 3 17:42:42 EDT 2004
Well, the random module is pretty easy to use.
Here is a an example:
>>> import random
>>> l = []
>>> for i in range(10):
.. l.append(int(random.random()*10))
..
>>> l
[4, 2, 0, 3, 7, 8, 5, 9, 9, 8]
>>>
random.random() gives a floating point number between 0 and 1, so I just
multiplied it by 10 to get from 0-10 then cast it to an integer with int().
To get a more accurate representation (since int() truncates the argument)
you could use int(round(...)) instead.
HTH,
Glen
----- Original Message -----
From: Dragonfirebane at aol.com
To: tutor at python.org
Sent: Friday, June 04, 2004 7:32 AM
Subject: [Tutor] random number equations . . .
I don't want to write my own equations for creating random numbers (based on
'time.time()'), but i need random numbers to create account id's and assign
starting money in the program segment below . . . i would use [import random
*] but i'm not sure which section of random is a random number generator or
the syntax necessary to apply it. Any help would be appreciated.
def newuser():
new = raw_input("Are you a new user [y/n]? ")
while new not in 'yYnN':
new = raw_input("""
Please enter 'y' for yes or 'n' for no.
Are you a new user? """)
if new in 'yY':
import math
vowelcount = 0
name = raw_input("Please enter your first name. ")
name = name[:6]
namel = raw_input("Please enter your last name. ")
namel = namel[:6]
name = name + ' ' + namel
vowels = ['a','e','i','o','u','A','E','I','O','U']
for letter in name:
if letter in vowels:
vowelcount = vowelcount + 1
global startmon
global tim
tim = time.time() % 100000
startmon = vowelcount * tim
startmon = math.sqrt(startmon) * 3141.5962
startmon = str(startmon)[:7]
startmon = int(float(startmon))
print "Welcome, %s. You have $%d to use in BankSim 1.0." % (name,
startmon)
def newacc():
global newac
newac = raw_input("Would you like to create a new account [y/n]? ")
while newac not in 'yYnN':
newac = raw_input("""
Please enter 'y' for yes or 'n' for no.
Would you like to create a new acount [y/n]? """)
if newac in 'yY':
import math
global newacid
newacid = int(raw_input("Please enter your birthday [ddmmyyyy]. "))
newacid = newacid * tim
newacid = math.sqrt(newacid) * 314.15692
newacid = str(newacid)[:6]
newacid = int(float(newacid))
initbal = int(raw_input("How much money would you like to put in
your account? "))
print "Account number %d created with $%d in it." % (newacid,
initbal)
mon = startmon - initbal
print "You now have $%d in account %d and $%d out of the bank." %
(initbal, newacid, mon)
Thanks,
Orri
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list