[Tutor] What does "random" in shuffle( x[, random]) do?

Luke Paireepinart rabidpoobear at gmail.com
Mon Sep 4 00:07:26 CEST 2006


[snip]

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?
>

no, it's not a random reordering.
As others have said already,
you can't determine the randomness of a function just by running it once.
Why do you think it's a random reordering?
If you ran it many times, you'd see why we've been saying that it's
important not to test it just once.

#--- example script.py
from random import shuffle
def a():
    return 0.5
def run_shuffle(lst):
    shuffle(lst,a)
    print lst

import copy
lst = [1,2,3,4]
for x in range(20):
    tmp = copy.copy(lst)
    run_shuffle(tmp)
#--- end

output:
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
[1, 4, 2, 3]
>>>

So yes, I have already given you an example where the second argument does
something.
Or do you still think that it's random? :)

Thanks,
>

You're welcome.

Dick Moores
>

-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060903/b4f53c30/attachment.htm 


More information about the Tutor mailing list