Randomness
Define a function, random_number
, that takes no parameters. The function must generate a random integer between 1
and 100
, both inclusive, and return it.
Calling the function multiple times should (usually) return different numbers.
For example, calling random_number()
some times might first return 42
, then 63
, then 1
.
Â
==== SOLUTION ====
You can implement the `random_number` function in Python using the `random` module, which provides functions for generating random numbers. Specifically, you can use the `randint()` function from the `random` module to generate a random integer within a specified range. Here’s how you can do it:
“`python
import random
def random_number():
     return random.randint(1, 100)
# Test the function
print(random_number()) # Output: Random number between 1 and 100
“`
Explanation:
1. `import random`: This line imports the `random` module, which contains functions for generating random numbers.
2. `def random_number():`: This line defines a function named `random_number` that takes no parameters.
3. `random.randint(1, 100)`: This line generates a random integer between 1 and 100 (inclusive) using the `randint()` function from the `random` module.
4. The function returns the generated random number.
So, when you call `random_number()`, it will return a different random number between 1 and 100 each time you call it.
Â