Hello everyone, I am currently working on MINMAX alogorithm on TIC-TAC-TOE, before that I am trying to code out a small idea, that might help me during minmax, here’s the code.
The goal is to randomly select each element at a time from one array and append to other array. I want to improve this . Provide your feedback,suggestions.
import random
def getRandom():
arr = ['random1','random2','random3','random4','random5','random6','random7','random8','random9','random10']
selected = []
selected.append(arr[9])
print(selected)
print(random.randint(0,len(arr)))
while len(selected) < 10:
if arr[random.randint(0,len(arr))] not in selected:
selected.append(arr[random.randint(0,len(arr))])
else :
print('Its already there')
print(selected)
getRandom()
random.randint() is a function that will return a random number every time you call it. So if you call it 3 times, you might get 3 different numbers → meaning if you want to work with the number, you need to save it in a variable.
Also in order to save time, you should remove the element from arr. Because if selected has 9 elements, your while-loop would on average take 10 runs to add the last element, despite the fact there is only one eligable element.
Also also, in oder to REALLY save time: did you check if the random-library already contains a method to create a random order?
What’s the problem? Please elaborate what issue you are encountering.
That’s like 50-100 lines of code and you didn’t encorporate a couple of suggestions I gave you to shorten it.
So, what do you expect us to do? Read it and give suggestions… on what?
Also consider uploading the code on something like Replit, so we could try it out.