Search code examples
pythonrandomnumbersgenerated

I need to generate x random numbers in an interval from 1 to x but each number have to occur only once


I need to generate a random number, actually i need 70128 random numbers form 1 to 70128. Here is what I'm using:

index = numpy.random.randint(1,70128,70128)

The other thing is, that I need each number between 1 and 70128 to be generated only once.

This means that I need a list of 70128 random generated numbers between 1 and 70128 but each number have to occur only once.


Solution

  • Use numpy's random.permutation function, which, if given a single scalar argument x, will return a random permutation of the numbers from 0 to x. For instance:

    np.random.permutation(10)
    

    Gives:

    array([3, 2, 8, 7, 0, 9, 6, 4, 5, 1])
    

    So, in particular, np.random.permutation(70128) + 1 does precisely what you'd like.