[Tutor] What does "random" in shuffle( x[, random]) do?
Alan Gauld
alan.gauld at freenet.co.uk
Sun Sep 3 18:04:12 CEST 2006
> "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()."
>
>>>> from random import shuffle, random
>>>> lst = ["a", "b", "c", "d"]
>>>> shuffle(lst)
>>>> shuffle(lst, random)
>
> I can't see that shuffle(a) is any different from shuffle(a,
> random). Is it? And how?
It isn't any different in this case. The docs point out that if you
don't provide a value then random is used. So by passing random
you are simpoly doing what the default behaviour does.
To see anything different try defining your own function that returns
a value between 0 and 1:
def r0(): return 0
def r1(): return 0.999999999)
Try using those values and see if the amount of randomness
in shuffles behaviour changes
for f in [r0,r1,random]:
print '-------------'
for n in range(3):
lst = ['a','b','c','d']
shuffle(lst,f)
print lst
Can you see how the function has a difference now?
Alan G.
More information about the Tutor
mailing list