Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • What does "random" in shuffle( x) do?

    6 answers - 63 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit


    Tutor maillist - Tutor (AT) python (DOT) org
  • No.1 | | 1736 bytes | |

    **** Moores wrote:
    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()."

    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?

    The docs say that shuffle(a) *is* the same as shuffle(a, random). If you
    don't supply a second argument, the function random() is used. So
    passing random as the second arg is the same as omitting the second arg.

    reason to provide your own random function would be if you have one
    that is more random than the standard function, for example os.urandom()
    or a function based on an external random source such as
    The random number generator in Python
    (Mersenne twister) is very high quality but that hasn't always been the
    case and it is still deterministic.

    You might be interested in the Wikipedia article:

    Trying the two versions once each, getting different results and saying
    you can't see that they are different isan interesting approach :-)
    But seriously, even with a poor random function you would have to call
    shuffle many times and analyze the entire body of results carefully to
    see any problem.

    Kent
    Thanks,

    **** Moores

    Tutor maillist - Tutor (AT) python (DOT) org

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.2 | | 1211 bytes | |

    "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.

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.3 | | 63 bytes | |


    Tutor maillist - Tutor (AT) python (DOT) org
  • No.4 | | 2312 bytes | |

    At 04:43 AM 9/3/2006, Kent Johnson wrote:
    >**** Moores wrote:

    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()."

    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?
    >
    >The docs say that shuffle(a) *is* the same as shuffle(a, random). If you
    >don't supply a second argument, the function random() is used. So
    >passing random as the second arg is the same as omitting the second arg.
    >

    reason to provide your own random function would be if you have one
    >that is more random than the standard function, for example os.urandom()
    >or a function based on an external random source such as

    The random number generator in Python
    >(Mersenne twister) is very high quality but that hasn't always been the
    >case and it is still deterministic.
    >
    >You might be interested in the Wikipedia article:
    >
    >
    >
    >Trying the two versions once each, getting different results and saying
    >you can't see that they are different isan interesting approach :-)


    Well, sure it's stupid if you know what "supplying your own random
    function in place of random.random()" means. I do now, thanks to you
    and Alan Gauld.

    >But seriously, even with a poor random function you would have to call
    >shuffle many times and analyze the entire body of results carefully to
    >see any problem.


    Because I'm content with the pseudo-randomness supplied by the
    current random.random(), I won't pursue my questions about that 2nd
    argument of shuffle() any longer.

    Thanks to all,

    **** Moores

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.5 | | 1797 bytes | |

    [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.
    do you still think that it's random? :)

    Thanks,

    You're welcome.

    **** Moores

    -Luke

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.6 | | 63 bytes | |


    Tutor maillist - Tutor (AT) python (DOT) org

Re: What does "random" in shuffle( x) do?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 7 + 7 =
QUESTION ON "Python"

EMSDN.COM