在 python 中获得一个新的随机数
Get a new randomized number in python
也许这是一个菜鸟问题,但是每次重复时我怎样才能得到一个新的随机数?
realport = random.randint(portstarts, portends)
liste = list(itertools.repeat('{}:{}:{}:{}'.format(hostname, realport, username, password), amount))
正如 ShadowRanger 评论的那样,itertools.repeat
可能不是您想要的。
“”可能是表达您想要的内容的更好方式:
def getport():
return random.randint(portstarts, portends)
liste = [
'{}:{}:{}:{}'.format(hostname, getport(), username, password)
for _ in range(amount)
]
如果您希望所有端口都是唯一的,那么您可以改为使用 random.sample
来“无替换采样”:
liste = [
'{}:{}:{}:{}'.format(hostname, port, username, password)
for port in random.sample(range(portstarts, portends+1), amount)
]
参见,例如How to incrementally sample without replacement? 用于在 Python
中进行无替换采样的各种方式
也许这是一个菜鸟问题,但是每次重复时我怎样才能得到一个新的随机数?
realport = random.randint(portstarts, portends)
liste = list(itertools.repeat('{}:{}:{}:{}'.format(hostname, realport, username, password), amount))
正如 ShadowRanger 评论的那样,itertools.repeat
可能不是您想要的。
“
def getport():
return random.randint(portstarts, portends)
liste = [
'{}:{}:{}:{}'.format(hostname, getport(), username, password)
for _ in range(amount)
]
如果您希望所有端口都是唯一的,那么您可以改为使用 random.sample
来“无替换采样”:
liste = [
'{}:{}:{}:{}'.format(hostname, port, username, password)
for port in random.sample(range(portstarts, portends+1), amount)
]
参见,例如How to incrementally sample without replacement? 用于在 Python
中进行无替换采样的各种方式