[Tutor] random number equations . . .

Dragonfirebane at aol.com Dragonfirebane at aol.com
Thu Jun 3 17:52:55 EDT 2004

```In a message dated 6/3/2004 5:43:27 PM Eastern Standard Time,
gew75 at hotmail.com writes:
Thanks.  Would this:

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
global rannum = []
name = name[:6]
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
for i in range(10):
rannum.append.int(round(random.random() * 1000000))
startmon = vowelcount * rannum[0]
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 = newacid * rannum[5]
newacid = str(newacid)[:6]
newacid = int(float(newacid))
initbal = int(raw_input("How much money would you like to put in
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)

work using your example? (The relevant portion is bolded).

Thanks again,
Orri
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)