[Tutor] Help using Random module
Bob Gailer
bgailer@alum.rpi.edu
Thu Apr 17 12:54:02 2003
--=======28D737AD=======
Content-Type: multipart/alternative; x-avg-checked=avg-ok-4F531F4E; boundary="=====================_8630489==.ALT"
--=====================_8630489==.ALT
Content-Type: text/plain; x-avg-checked=avg-ok-4F531F4E; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit
At 03:13 PM 4/16/2003 -0600, Danny wrote:
>Hi Everyone,
>
>My question is, I want to use the random module to pick a random number
>from 1,100 I read up on it but am very confused about how to use it
>properly. What I've tried to say is.
>
>def number(x):
> x = random.randrange(1,100)
> result = x
> return result
>
>guess = 0
>
>while guess != number:
> guess = input ("guess a number")
>
> if guess > number:
> print "Too High"
> print guess
>
> elif guess < number:
> print "too Low"
> print guess
>
>print "Just Right"
>
>When I run the program the random number is always greater then 100 what
>am I doing wrong?
"number" refers to a function object. guess < number will always return True.
"number()" will call the function. However if you replace number with
number() above, it will return a (probably) different value each time.
Perhaps what you want is:
guess = 0
num = number()
while guess != num:
and refer to num instead of number from there on.
>Also what does it mean when the module has a uppercase letter. For example
>random.Random is that the same as random.random??
Names in Python are case sensitive.
A little experimentation:
>>> import random
>>> random.Random
<class random.Random at 0x011516C0>
>>> random.random
<bound method Random.random of <random.Random instance at 0x011527B8>>
>>> help(random.random)
Help on method random in module random:
random(self) method of random.Random instance
Get the next random number in the range [0.0, 1.0).
>>> help(random.Random)
Help on class Random in module random:
class Random
| Random number generator base class used by bound module functions.
[snip]
Bob Gailer
bgailer@alum.rpi.edu
303 442 2625
--=====================_8630489==.ALT
Content-Type: text/html; x-avg-checked=avg-ok-4F531F4E; charset=us-ascii
Content-Transfer-Encoding: 8bit
<html>
<body>
At 03:13 PM 4/16/2003 -0600, Danny wrote:<br><br>
<blockquote type=cite class=cite cite><font face="arial" size=2>Hi
Everyone,</font><br>
<br>
<font face="arial" size=2>My question is, I want to use the random module
to pick a random number from 1,100 I read up on it but am very confused
about how to use it properly. What I've tried to say is. </font><br>
<br>
<font face="arial" size=2>def number(x):<br>
x = random.randrange(1,100)<br>
result = x<br>
return result<br>
<br>
guess = 0</font><br>
<br>
<font face="arial" size=2>while guess != number:<br>
guess = input ("guess a
number")</font><br>
<br>
<font face="arial" size=2> if guess > number:<br>
print "Too
High"<br>
print guess</font><br>
<br>
<font face="arial" size=2> elif guess <
number:<br>
print "too
Low"<br>
print guess</font><br>
<br>
<font face="arial" size=2>print "Just Right"<br>
</font> <br>
<font face="arial" size=2>When I run the program the random number is
always greater then 100 what am I doing wrong? </font></blockquote><br>
"number" refers to a function object. guess < number will
always return True.<br>
"number()" will call the function. However if you replace
number with number() above, it will return a (probably) different value
each time.<br><br>
Perhaps what you want is:<br><br>
guess = 0<br>
num = number()<br>
while guess != num:<br><br>
and refer to num instead of number from there on.<br><br>
<blockquote type=cite class=cite cite><font face="arial" size=2>Also what
does it mean when the module has a uppercase letter. For example
random.Random is that the same as random.random??
</font></blockquote><br>
Names in Python are case sensitive.<br><br>
A little experimentation:<br>
>>> import random<br>
>>> random.Random<br>
<class random.Random at 0x011516C0><br>
>>> random.random<br>
<bound method Random.random of <random.Random instance at
0x011527B8>><br>
>>> help(random.random)<br>
Help on method random in module random:<br>
random(self) method of random.Random instance<br>
Get the next random number in the range [0.0,
1.0).<br>
>>> help(random.Random)<br>
Help on class Random in module random:<br>
class Random<br>
| Random number generator base class used by bound module
functions.<br>
[snip]<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>
--=====================_8630489==.ALT--
--=======28D737AD=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-4F531F4E
Content-Disposition: inline
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 4/1/2003
--=======28D737AD=======--