Reputation: 67
I want to generate a random list with repetition and I do not know how it is done, because it always gives me error, I write this:
I want a list of 2000 numbers between 1 and 100
Help me pls.
Upvotes: 2
Views: 7528
Reputation: 4305
You can just use the random library and a list comprehension.
import random
[random.randint(1,100) for x in range(2000)]
If you'd like to print that list, you can assign it a value and print it as you've done in your question.
import random
x = [random.randint(1,100) for x in range(2000)]
print x
EDIT: Based on OP's comment below: If you'd like to remove all values under 50, you can do it while you're generating the initial list and not generate them at all.
import random
[random.randint(50,100) for x in range(2000)]
Upvotes: 7