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 from Two-Bit Alchemist on Stack OverflowVideos
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
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)
Looking to create a random number between 1 and 100. Actually 0 (zero) is valid too, so between 0-100.
Whats your preferred way of dong that in Python?