import random
L = range(1, 1000000)
random.shuffle(L) # shuffles in-place
on Python3, you will need to use
L = list(range(1, 1000000))
random.shuffle(L) # shuffles in-place
Question
I'd like to create a million unordered list, xrange(1, 1000000)
for eg gives me an ordered list. I think I need something like xrange but generates it in an unordered fashion. I think I can generate a list manually by looping around random.randint
and some manual checks to guarantee uniqueness of number in list but I reckon it would be time consuming. Any ideas?
Solution
import random
L = range(1, 1000000)
random.shuffle(L) # shuffles in-place
on Python3, you will need to use
L = list(range(1, 1000000))
random.shuffle(L) # shuffles in-place
OTHER TIPS
Use the random
module from NumPy. Specifically np.random.permutation
>>> import numpy as np
>>> np.random.permutation(5)
array([2, 1, 0, 3, 4])
If you're using numpy
you can use permutation
:
numpy.random.permutation(10)
>> array([1, 7, 6, 0, 5, 9, 2, 3, 8, 4])