At 01:51 AM 9/3/2006, Luke Paireepinart wrote:


On 9/3/06, Dick Moores <[EMAIL PROTECTED]> wrote:
http://docs.python.org/lib/module-random.html says,

"shuffle( x[, random])
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random()."


it means you can pass in your own random function.
for example:
def a:
    return  0.5

shuffle(lst, a)

will call the 'a' function instead of using the function called ' random.random',
and since 'a' just returns a constant the results won't be random.
It's letting you define your own random number-generating function if you want.
it just tells you it's 'zero-argument' so you know that shuffle(lst, funcname) results in
the call funcname() with no arguments.
If your random function required arguments you could probably use lambda somehow to
pass the arguments into it.
HTHT,
-Luke

>>> from random import shuffle, random
>>> lst = ["a", "b", "c", "d"]
>>> shuffle(lst)
>>> lst
['c', 'b', 'd', 'a']
>>> shuffle(lst, random)
>>> lst
['d', 'c', 'b', 'a']
>>>

I can't see that shuffle(a) is any different from shuffle(a, random). Is it? And how?


No it's not because using the function 'random.random' is the default  :)

Ah, I'd forgotten that in shuffle( x[, random], "random" would be the default. But please bear with me. Using your function a, I wrote testShuffle.py:

# testShuffle.py
from random import *
def a():
    return 0.5
lst = ['1', '2', '3', '4']
shuffle(lst,a)
print lst

>>>
['1', '4', '2', '3']
>>>

Again, this just the random reordering of lst in place. Could you show me a little script where the 2nd argument of shuffle actually does something?

Thanks,

Dick Moores


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to