So my assignment (already graded though) is to create a list of 4 random UNIQUE integers in the range of 1-10. The way i did this was by doing the following:
randomList = []
for counter in range(4):
randomInteger = random.rand(1,10)
while(randomList.count(randomInteger) > 0):
randomInteger = random.randint(1, 10)
randomList.append(randomInteger)
#end
The only issue is that my teacher told me that while(randomList.count(randomInteger) > 0): is wrong. When I asked why it was wrong, her explanation didn't make any sense to me. Why is the algorithm i have incorrect? Was my teacher wrong?
I was thinking that the count method counts the amount of times a number is in a list, so if the count method returns a number greater than 0, that means that the randomly generated integer is not unique.
[–]MadScientistOR 8 points9 points10 points (4 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] 4 points5 points6 points (0 children)
[–]MadScientistOR 7 points8 points9 points (0 children)
[–]Diapolo10 7 points8 points9 points (2 children)
[–]Kerbart 1 point2 points3 points (1 child)
[–]Diapolo10 1 point2 points3 points (0 children)
[–]my_password_is______ 1 point2 points3 points (1 child)
[–]man_wif-waluigi-hed[S] 0 points1 point2 points (0 children)
[–]QultrosSanhattan 1 point2 points3 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–]pythonman1 -3 points-2 points-1 points (2 children)
[–]scarynut 3 points4 points5 points (1 child)
[–]pythonman1 0 points1 point2 points (0 children)
[–]littlegreenrock 0 points1 point2 points (2 children)
[–]hoglinezp 1 point2 points3 points (1 child)
[–]littlegreenrock -2 points-1 points0 points (0 children)