How to generate non-repeating random numbers in Python?

PythonServer Side ProgrammingProgramming

Following program generates 10 random, non-repetitive integers between 1 to 100. It generates a random integer in the given interval and adds it in a list if it is not previously added.

>>> import random
>>> list=[]
>>> for i in range(10):
          r=random.randint(1,100)
          if r not in list: list.append(r)

>>> list
[13, 53, 25, 95, 64, 87, 27, 93, 74, 60]
raja
Published on 01-Mar-2018 10:24:33
Advertisements