python random.Random
时间: 2023-10-14 14:21:23 浏览: 114
python中随机函数random用法实例
The random.Random class in Python is used to generate random numbers. It is a pseudorandom number generator that uses a deterministic algorithm to generate a sequence of numbers that appear to be random. The class provides methods to generate random numbers of different types, such as integers, floats, and boolean values. It also allows you to set the seed value to generate the same sequence of random numbers every time you run the program.
Here is an example of how to use the random.Random class to generate a random integer between 1 and 100:
```
import random
# create a new instance of the Random class with a seed value of 42
rng = random.Random(42)
# generate a random integer between 1 and 100
random_int = rng.randint(1, 100)
print(random_int)
```
In this example, we first import the random module and create a new instance of the Random class with a seed value of 42. We then use the `randint()` method of the `rng` object to generate a random integer between 1 and 100. Finally, we print the generated random integer. Since we have set the seed value to 42, we will always get the same random integer when we run this program.
阅读全文