This article describes how to generate random values without duplicates in Python. The example analyzes the implementation skills of Python Algorithm Implementation and Python built-in method, which is very simple and practical, for more information about how to generate random values without repeating Python, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
Here, random values that are not repeated are generated from the list.
The algorithm is implemented as follows:
Import randomtotal = 100li = [I for I in range (total)] res = [] num = 20for I in range (num): t = random. randint (I, total-1) res. append (li [t]) li [t], li [I] = li [I], li [t] print res
In fact, python has implemented this method:
Print random. sample (li, num)
I hope this article will help you with Python programming.