'Randomizing Sentences In Python
I not long ago finished my project which comments on a video based on a keyword on YouTube, it will pick a random comment using the random library. The program has been acting strange when "randomizing" the comments to add to the YouTube videos. I'm starting to think I may need to improve the randomness, the fact being that it has chosen the 4th comment (out of 9) 7 times, and the 9th one 3 times. Both of these results occur after each other, in other words, it is sending the same message in a row when it's meant to pick randomly and not repeat. Is there any way I can increase how random this is? If you know, please do tell me, I will appreciate it a bunch!
PS: This is more of stopping it from repeating the same sentence on YouTube.
code:
if __name__ == "__main__":
from googleapiclient.errors import HttpError
import random
import time
import sys
# Comments are getting loaded
comments = load_comments('Comments.txt')
# Getting the number of comments you want to add
number_of_comments = int(input('Enter the number of comments: '))
count, cycle, videoid_store = 0, 1, []
# Getting the keyword
keyword = input('Enter the Keyword: ')
# This loop keeps running until all comments have been added
youtube = authentication()
while count < number_of_comments:
print("Searching for videos .. (Cycle:%d)" %cycle)
time.sleep(10)
random.shuffle(comments)
Solution 1:[1]
If you want to keep the randomness but avoid repeats or frequent re-occurrences, you have to remember the previous outcomes, to manipulate the odds in favour of the ones that have occurred less frequently so far.
Some examples:
- Shuffle the outcomes. Go through all, by the order obtained. Repeat.
- Keep a count of the last few outcomes. If we get an outcome that has been drawn X times already, draw again up to X times.
Code, for the above examples:
import random
import collections
class ShuffleAndExhaust(): # cycle in random order each time
def __init__(self,N): # N: number of possible outcomes
self.N = N
self.queue = list()
def draw(self):
if not self.queue: # exhausted (or 1st ever draw)
self.queue.extend(range(self.N))
random.shuffle(self.queue)
return self.queue.pop()
class RememberAndRetry(): # re-draw, if reoccurring
def __init__(self,N): # N: number of possible outcomes
self.N = N
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: # drew this number N times already
triesleft[n] -= 1 # 1 less try left drawing this same number until we accept it
else: break
self.history.append(n)
return n
Testing 100 draws of 10 possible outcomes:
>>> 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
Use N = the number of comments and each draw as the index of the comment to pick.
Solution 2:[2]
Try random.choice()
x=random.choice(['option1','option2'])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |