利用 random 里的 seed 在需要的时候(测试阶段)确保每次生成的随机数相同
利用 random 里的 randint 指定范围
from random import seed, randint
import sys
try:
get_seed = int(input('Give the seed for random with an integer: '))
except ValueError:
print('Error here. Input is not an integer, giving up.')
sys.exit()
# prompts the user a strictly positive number, nb_of_elements.
try:
nb_of_elements = int(input('How many elements do you want to generate? '))
except ValueError:
print('Error here. Input is not an integer.')
sys.exit()
if nb_of_elements <= 0:
print('Error here. Input should be strictly positive.')
sys.exit()
seed(get_seed)
# Generates a list of nb_of_elements random integers between -50 and 50.
L = [randint(-50, 50) for _ in range(nb_of_elements)]
# Prints out the list.
print('nThe list is:' , L)
运行得到(输入seed = 5, 生成 4 个随机数)
Give the seed for random with an integer: 5
How many elements do you want to generate? 4
The list is: [29, -18, 44, -5]



