[Tutor] What does "random" in shuffle( x[, random]) do?
Dick Moores
rdm at rcblue.com
Sun Sep 3 22:16:16 CEST 2006
At 04:43 AM 9/3/2006, Kent Johnson wrote:
>Dick Moores 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()."
> >
> > >>> 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.
>
>One 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
>http://www.fourmilab.ch/hotbits/. 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:
>http://en.wikipedia.org/wiki/Random_number_generator
>http://en.wikipedia.org/wiki/Mersenne_twister
>
>Trying the two versions once each, getting different results and saying
>you can't see that they are different is...an 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,
Dick Moores
More information about the Tutor
mailing list