[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>
&nbsp;<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>
&nbsp;<br>
<font face="arial" size=2>def number(x):<br>
&nbsp;&nbsp;&nbsp; x = random.randrange(1,100)<br>
&nbsp;&nbsp;&nbsp; result = x<br>
&nbsp;&nbsp;&nbsp; return result<br>
&nbsp;&nbsp;&nbsp; <br>
guess = 0</font><br>
&nbsp;<br>
<font face="arial" size=2>while guess != number:<br>
&nbsp;&nbsp;&nbsp; guess = input (&quot;guess a 
number&quot;)</font><br>
&nbsp;<br>
<font face="arial" size=2>&nbsp;&nbsp;&nbsp; if guess &gt; number:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Too
High&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print guess</font><br>
&nbsp;<br>
<font face="arial" size=2>&nbsp;&nbsp;&nbsp; elif guess &lt; 
number:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;too 
Low&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print guess</font><br>
&nbsp;<br>
<font face="arial" size=2>print &quot;Just Right&quot;<br>
</font>&nbsp;<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>
&quot;number&quot; refers to a function object. guess &lt; number will
always return True.<br>
&quot;number()&quot; 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>
&gt;&gt;&gt; import random<br>
&gt;&gt;&gt; random.Random<br>
&lt;class random.Random at 0x011516C0&gt;<br>
&gt;&gt;&gt; random.random<br>
&lt;bound method Random.random of &lt;random.Random instance at
0x011527B8&gt;&gt;<br>
&gt;&gt;&gt; help(random.random)<br>
Help on method random in module random:<br>
random(self) method of random.Random instance<br>
&nbsp;&nbsp;&nbsp; Get the next random number in the range [0.0,
1.0).<br>
&gt;&gt;&gt; help(random.Random)<br>
Help on class Random in module random:<br>
class Random<br>
&nbsp;|&nbsp; 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=======--