Re: [Tutor] Beginner - understanding randint arguments

2014-02-15 Thread Jay Lozier


On 02/15/2014 11:25 AM, Marc Eymard wrote:

Hello Tutor,

I need to generate a random integer between 0 and 100.

The range is supposed to be adjusted by my two variables:
low_range and high_range.

The logic of using the variables as part of the function arguments is 
to manage to get a smaller range each time the function is called 
_excluding_ the possible repeat of the return value of randint.


Here is what happens in my script:

 import random
 low_range = -1
 high_range = 101
 random.randint(low_range + 1, high_range - 1)
56
 low_range
-1
 high_range
101*
*


I was rather expecting:

  low_range

0

 high_range

100


Can somebody explain why both low_range and high_range are still 
returning their initial values ?


Thanks,
Marc*
*


The variables low_range and high_range are not modified. The arguments 
to random.randint do not change the initial values of the variables, 
only the actual values passed.


--
Jay Lozier
jsloz...@gmail.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner - understanding randint arguments

2014-02-15 Thread Alan Gauld

On 15/02/14 16:25, Marc Eymard wrote:


Here is what happens in my script:

  import random
  low_range = -1
  high_range = 101
  random.randint(low_range + 1, high_range - 1)
56
  low_range
-1
  high_range
101*

I was rather expecting:

   low_range
0
  high_range
100


Really? Why?
You never change low_range or high_range so why would
their values change?

The help() for randint says:

randint(self, a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.

So the function doesn't change the values either it
just returns a random number between them.


Can somebody explain why both low_range and high_range are still
returning their initial values ?


Because you didn't change them.
When you called randint you passed in two expressions:

low_range+1 and high_range-1
which Python evaluated as 0 and 100.

But that did not change your variable values in any way, it just created 
new values that were passed into randint() as 'a' and 'b' respectively.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner - understanding randint arguments

2014-02-15 Thread Steven D'Aprano
On Sat, Feb 15, 2014 at 04:25:34PM +, Marc Eymard wrote:

 Can somebody explain why both low_range and high_range are still 
 returning their initial values ?

Because you haven't changed either of them. Imagine the chaos if every 
time you did arithmetic on a variable, Python changed the variable:

x = 1
y = x + 100

What is the value of y? 101, correct?

What is the value of x? It should still be 1, not 101.

The same applies when you get rid of the y = and just pass it to a 
function:

x = 1
some_function(x + 100)

What's the value of x? It needs to be 1, because you haven't changed it.

The only way to change x is to explicitly change it:

x = x + 100


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor