The Python Oracle

Generate 'n' unique random numbers within a range

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Jungle Looping

--

Chapters
00:00 Question
00:56 Accepted answer (Score 566)
01:35 Answer 2 (Score 32)
02:03 Answer 3 (Score 17)
02:28 Answer 4 (Score 8)
03:23 Thank you

--

Full question
https://stackoverflow.com/questions/2284...

Accepted answer links:
[random.sample]: https://docs.python.org/2/library/random...

Answer 2 links:
[random.sample]: https://docs.python.org/2/library/random...

Answer 3 links:
[set]: https://docs.python.org/2/library/stdtyp...

Answer 4 links:
[standard library]: https://docs.python.org/2/library/random...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #random #unique

#avk47



ACCEPTED ANSWER

Score 590


If you just need sampling without replacement:

>>> import random
>>> random.sample(range(1, 100), 3)
[77, 52, 45]

random.sample takes a population and a sample size k and returns k random members of the population.

If you have to control for the case where k is larger than len(population), you need to be prepared to catch a ValueError:

>>> try:
...   random.sample(range(1, 2), 3)
... except ValueError:
...   print('Sample size exceeded population size.')
... 
Sample size exceeded population size



ANSWER 2

Score 33


Generate the range of data first and then shuffle it like this

import random
data = list(range(numLow, numHigh))
random.shuffle(data)
print data

By doing this way, you will get all the numbers in the particular range but in a random order.

But you can use random.sample to get the number of elements you need, from a range of numbers like this

print random.sample(range(numLow, numHigh), 3)



ANSWER 3

Score 16


You could add to a set until you reach n:

setOfNumbers = set()
while len(setOfNumbers) < n:
    setOfNumbers.add(random.randint(numLow, numHigh))

Be careful of having a smaller range than will fit in n. It will loop forever, unable to find new numbers to insert up to n




ANSWER 4

Score 10


You could use the random.sample function from the standard library to select k elements from a population:

import random
random.sample(range(low, high), n)

In case of a rather large range of possible numbers, you could use itertools.islice with an infinite random generator:

import itertools
import random

def random_gen(low, high):
    while True:
        yield random.randrange(low, high)

gen = random_gen(1, 100)
items = list(itertools.islice(gen, 10))  # Take first 10 random elements

After the question update it is now clear that you need n distinct (unique) numbers.

import itertools
import random

def random_gen(low, high):
    while True:
        yield random.randrange(low, high)

gen = random_gen(1, 100)

items = set()

# Try to add elem to set until set length is less than 10
for x in itertools.takewhile(lambda x: len(items) < 10, gen):
    items.add(x)