Is it possible to customize the random function to avoid too much repetition of words? [duplicate]

2024/11/24 13:11:02

Theoretically, when randomization is used, the same word may be printed several consecutive times, or it may be printed repeatedly (for example, 3 times out of 5 attempts the same word may be printed). I know, this is correct, this is right and normal.

I would like to set the same value of a variable to be printed again only after four times, therefore on the fifth attempt. I simply want to set some sort of "block" for each value and this block must last for four print attempts.

For example, in my code, I would like in the a_random variable, if Word A2 is printed, then Word A2 will only be able to print again after four subsequent printing attempts, but I want to print only one random word among the four (and not print four words).

For example, if I print Word A2, the subsequent prints must be 4 random among "Word A1", "Word A3", "Word A4", "Word A5", "Word A6", "Word A7", "Word A8", then on the fifth attempt it will be able to print Word A2 again (but not mandatory, because in addition to Word A2 it will be possible to print any word).

Important: I want to print only one word at a time. I don't want to print four words or more words at a time. The output I would like is only one word.

import random
import tkinter as tk
from tkinter import ttkroot = tk.Tk()
root.geometry('300x200')def func():a = "Word A1", "Word A2", "Word A3", "Word A4", "Word A5", "Word A6", "Word A7", "Word A8"print("NEW")a_random = print(random.choice(a))print(" ")  return a_randombutton = ttk.Button(root, text='Random', command=func)
button.place(x=10, y=10)root.mainloop()
Answer

This uses a list that keeps track of the blacklisted indices and removes those from sampling. The list will not grow larger than four items.

import random
import numpy as npvocab = np.array(["Word A1","Word A2","Word A3","Word A4","Word A5","Word A6","Word A7","Word A8"
])# a list of all indices of the array
indices = np.arange(0, len(vocab))# a list that will store blacklisted indices as long as they are forbidden (4 time steps)
blacklist = []for i in range(10):# only allow values that were not used in the last for stepsrandom_index = random.choice(list(set(indices) - set(blacklist)))# update blacklist for next runblacklist = [random_index] + blacklist  # add new index at the beginningblacklist = blacklist[0:4]  # only keep four values, remove older itemsprint(vocab[random_index])
https://en.xdnf.cn/q/120533.html

Related Q&A

country convert to continent

def country_to_continent(country_name):country_alpha2 = pc.country_name_to_country_alpha2(country_name)country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)country_continent_name…

Iterate through a list and delete certain elements

Im working on an assignment in my computer class, and Im having a hard time with one section of the code. I will post the assignment guidelines (the bolded part is the code Im having issues with):You a…

How to count numbers in a list via certain rules?

Just to say I have a str and a list of strs and I want to count how many strs in the list that is contained in a str. Is there an elegant way to do that?For example, l = {"foo", "bar&qu…

Count character occurrences in a Python string

I want to get the each character count in a given sentence. I tried the below code and I got the count of every character but it displays the repeated characters count in the output. How to delete repe…

The Concept Behind itertoolss product Function

so basically i want to understand the concept of product() function in itertools. i mean what is the different between yield and return. And can this code be shorten down anyway.def product1(*args, **k…

how to read data from multiple sheets in a single csv file using python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Quality Center: Set a Step Field in Python

I have a very simple problem here. I want to achieve the following VB script Code in Python:- dim objSfact dim objOrun dim mystep Set objOrun = QCutil.CurrentRun Set objSfact = objOrun.StepFactory…

Convert for loop from Python to C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Storing values in a CSV file into a list in python

Id like to create a list that stores all values of a single column. For example lets say my file has a column called FirstNames and for the first 3 rows, the names column has Merry, Pippin, Frodo.Id l…

Python arithmetic quiz task 1

I have no idea why this code is not working, as you can see Im trying to ask the user 10 questions display their score at the end. Everything works except that the score will always appear as a 0 or 1 …