Home > Article > Backend Development > How to generate 10 different random numbers in python
How python generates 10 different random numbers: first create an empty list of result; then loop until the length of result exceeds 10 and exit the loop, and randomly generate a number between 1-10 in the loop; finally Check whether the generated num random number is in the result list, if not, add it.
【Related learning recommendations: python tutorial】
Python's method of generating 10 different random numbers:
1. The first
range(1,10) generates a 1-10 list between, and then use the sample method of random. Choose 10 different numbers from the sequence.
2. The second type
Create an empty list of results, and then loop until the length of result exceeds 10 to exit the loop, and randomly generate a For numbers between 1-10, first check whether the generated num random number is in the result list, and add it if not.
3. The third method
first uses the range() method to generate a sequence of 1-10, and then uses the shuffle method to generate the sequence expression of temp. Create an unordered list, and then use list generation to directly loop through to a new list.
The above is the detailed content of How to generate 10 different random numbers in python. For more information, please follow other related articles on the PHP Chinese website!