Python Function to Generate Random Numbers Between 10 and 20
Here's a Python function named 'random_number' that returns a random number between 10 and 20:
import random
def random_number():
return random.randint(10, 20)
# Example usage
result = random_number()
print(result)
In this code:
- We import the 'random' module, which offers various functions for generating random numbers.
- The 'random_number' function uses the 'randint' function from the 'random' module. 'randint' generates a random integer within the specified range (inclusive), in this case, between 10 and 20.
- The function returns the generated random number.
You can test the function by calling it and printing the result as shown in the example usage. Each time you run the code, it will produce a different random number between 10 and 20.
原文地址: http://www.cveoy.top/t/topic/bv1f 著作权归作者所有。请勿转载和采集!