Trying to use sets for random selection, but the pop() method returns items in order

Paul Rubin http
Wed Jul 1 21:37:38 EDT 2009


Carl Banks <pavlovevidence at gmail.com> writes:
> If you're iterating through the whole list and don't need to preserve
> the original order (as was the case here) random.shuffle() is better.

1. Random.sample avoids iterating through the whole list when it can.

2. Say you want to choose 10 random numbers between 1 and 1000000.

   random.sample(xrange(1000000), 10)

works nicely.  Doing the same with shuffle is painful.



More information about the Python-list mailing list