Home  >  Article  >  Backend Development  >  How to generate 10 different random numbers in python

How to generate 10 different random numbers in python

coldplay.xixi
coldplay.xixiOriginal
2020-09-02 09:50:1336174browse

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.

How to generate 10 different random numbers in python

【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.

How to generate 10 different random numbers in python

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.

How to generate 10 different random numbers in python

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.

How to generate 10 different random numbers in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn