Python中的随机化语句
我不久前完成了我的项目,该项目基于YouTube上的关键字对视频进行评论,它将使用随机库选择随机评论。 该程序在将评论“随机”添加到 YouTube视频。我开始认为我可能需要改进随机性,事实上 它已经选择了第4条评论(9条评论中的第4条)7次,,第9条评论选择了3次。这两个 结果在一个接一个地出现,换句话说,它在一行中发送相同的消息 当它意味着随机挑选,而不是重复。 有没有办法增加随机性?如果你知道,请告诉我,我会非常感激的 PS:这更多的是阻止它在YouTube上重复同一句话 代码:Python中的随机化语句,python,python-3.x,random,youtube,youtube-api,Python,Python 3.x,Random,Youtube,Youtube Api,我不久前完成了我的项目,该项目基于YouTube上的关键字对视频进行评论,它将使用随机库选择随机评论。 该程序在将评论“随机”添加到 YouTube视频。我开始认为我可能需要改进随机性,事实上 它已经选择了第4条评论(9条评论中的第4条)7次,,第9条评论选择了3次。这两个 结果在一个接一个地出现,换句话说,它在一行中发送相同的消息 当它意味着随机挑选,而不是重复。 有没有办法增加随机性?如果你知道,请告诉我,我会非常感激的 PS:这更多的是阻止它在YouTube上重复同一句话 代码: 如果名称
如果名称=“\uuuuu main\uuuuuuuu”:
从GoogleAppClient.errors导入HttpError
随机输入
导入时间
导入系统
#评论正在加载
comments=加载注释('comments.txt')
#获取要添加的注释数
注释数=int(输入('输入注释数:'))
计数,循环,videoid_存储=0,1,[]
#获取关键字
关键字=输入('输入关键字:')
#此循环将一直运行,直到添加了所有注释
youtube=身份验证()
当计数<注释的数量时:
打印(“搜索视频..(周期:%d)”%Cycle)
时间。睡眠(10)
随机。随机(注释)
试试random.choice()
这是(平均)随机的
如果您希望保持随机性,但避免重复/频繁再次出现,则必须保留以前结果的历史记录,以便您可以将几率控制在较低频率的结果上
具体例子:
import random
import collections
class ShuffleAndExhaust(): # cycle in random order each time
def __init__(self,N):
self.N = N # number of possible outcomes
self.queue = list()
def draw(self):
if not self.queue:
self.queue.extend(range(self.N))
random.shuffle(self.queue)
return self.queue.pop()
class RememberAndRetry(): # re-draw in case of reoccurring outcomes
def __init__(self,N):
self.N = N # number of possible outcomes
self.history = collections.deque(maxlen=3*self.N) # maxlen = rule-of-thumb constant * N
def draw(self):
triesleft = collections.Counter(self.history)
while True:
n = random.randint(0,self.N-1)
if triesleft[n] > 0:
triesleft[n] -= 1
else: break
self.history.append(n)
return n
import random
import collections
class ShuffleAndExhaust(): # cycle in random order each time
def __init__(self,N):
self.N = N # number of possible outcomes
self.queue = list()
def draw(self):
if not self.queue:
self.queue.extend(range(self.N))
random.shuffle(self.queue)
return self.queue.pop()
class RememberAndRetry(): # re-draw in case of reoccurring outcomes
def __init__(self,N):
self.N = N # number of possible outcomes
self.history = collections.deque(maxlen=3*self.N) # maxlen = rule-of-thumb constant * N
def draw(self):
triesleft = collections.Counter(self.history)
while True:
n = random.randint(0,self.N-1)
if triesleft[n] > 0:
triesleft[n] -= 1
else: break
self.history.append(n)
return n
测试10种可能选择中的100种:
>>> random.seed(0) # for reproducibility in testing only
>>> r = ShuffleAndExhaust(10)
>>> print(*(r.draw() for i in range(100)))
6 9 0 2 4 3 5 1 8 7 5 3 2 7 1 0 6 8 4 9 4 1 8 6 5 2 3 9 0 7 5 6 9 4 7 1 3 8 2
0 0 8 9 7 5 3 6 2 1 4 5 3 9 7 0 1 4 6 2 8 8 7 1 0 2 4 3 6 9 5 8 4 1 9 2 6 7 5
3 0 7 1 6 2 4 8 9 0 3 5 2 0 4 3 8 5 1 7 9 6
>>> r = RememberAndRetry(10)
>>> print(*(r.draw() for i in range(100)))
1 8 6 4 3 9 7 5 1 3 0 2 0 9 1 0 4 6 2 7 8 5 9 3 6 7 0 1 4 5 2 5 4 8 7 6 3 1 7
9 8 9 4 0 7 9 1 0 8 6 1 5 3 2 5 2 3 6 6 2 7 4 0 4 1 8 6 7 0 1 5 3 9 8 0 9 5 4
7 7 1 6 8 3 8 5 2 3 9 0 5 0 2 6 2 1 4 4 7 4
使用N=评论的数量和每个绘图作为要选择的评论的索引。您能帮我解决这个问题吗?我还没有机会了解选择的基本原理。(它可以去哪里以及如何使用)您可以将注释存储在列表中,并使用random.choice将注释存储在变量中
>>> random.seed(0) # for reproducibility in testing only
>>> r = ShuffleAndExhaust(10)
>>> print(*(r.draw() for i in range(100)))
6 9 0 2 4 3 5 1 8 7 5 3 2 7 1 0 6 8 4 9 4 1 8 6 5 2 3 9 0 7 5 6 9 4 7 1 3 8 2
0 0 8 9 7 5 3 6 2 1 4 5 3 9 7 0 1 4 6 2 8 8 7 1 0 2 4 3 6 9 5 8 4 1 9 2 6 7 5
3 0 7 1 6 2 4 8 9 0 3 5 2 0 4 3 8 5 1 7 9 6
>>> r = RememberAndRetry(10)
>>> print(*(r.draw() for i in range(100)))
1 8 6 4 3 9 7 5 1 3 0 2 0 9 1 0 4 6 2 7 8 5 9 3 6 7 0 1 4 5 2 5 4 8 7 6 3 1 7
9 8 9 4 0 7 9 1 0 8 6 1 5 3 2 5 2 3 6 6 2 7 4 0 4 1 8 6 7 0 1 5 3 9 8 0 9 5 4
7 7 1 6 8 3 8 5 2 3 9 0 5 0 2 6 2 1 4 4 7 4