Python: Number ranges that are extremely large?
Question: 
    val = long(raw_input("Please enter the maximum value of the range:")) + 1
    start_time = time.time()
    numbers = range(0, val)
    shuffle(numbers)
    

I cannot find a simple way to make this work with extremely large inputs - can
anyone help?

I saw a question like this - but I could not implement the range function they
described in a way that works with shuffle. Thanks.


Answer: To get a random permutation of the range `[0, n)` in a memory efficient
manner; you could use
[`numpy.random.permutation()`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.permutation.html#numpy.random.permutation):

    
    
    import numpy as np
    
    numbers = np.random.permutation(n)
    

If you need only small fraction of values from the range e.g., to get `k`
random values from `[0, n)` range:

    
    
    import random
    from functools import partial
    
    def sample(n, k):
        # assume n is much larger than k
        randbelow = partial(random.randrange, n)
        # from random.py
        result = [None] * k
        selected = set()
        selected_add = selected.add
        for i in range(k):
            j = randbelow()
            while j in selected:
                j = randbelow()
            selected_add(j)
            result[i] = j
        return result
    
    
    print(sample(10**100, 10))
    

