# Q：for循环中不需要的随机迭代器

I tried to create a function for generating a set number (numbersToChoose) of values between two other values (startFrom and stopAt) but for some reason the iterator (?) in the second for-loop (line 7), in this case a seems to be randomly generated even though I don't see any reason for that.

``````def chooseRandomNumbers(numbersToChoose, startFrom, stopAt):

numbers = [(randrange(startFrom, stopAt))]  # initialize empty list of numbers

for n in range(numbersToChoose-1):  # choose random number from 1 to 49 a total of 7 times
x = randrange(startFrom, stopAt+1)
for a in numbers:  # check for already existing elements in "numbers"
if x == numbers[a]:  # if new random number already exists in "numbers" discard and run loop again
n -= 1  # decreases n by 1 so a new (hopefully not already existing) number can be generated
else:  # generated numbers does not exist in "numbers" yet
numbers.append(x)  # appends randomly chosen number to list "numbers"

return numbers  # returns said list "numbers"
``````

Any advice on how to deal with this is greatly appreciated. Also please tell me if possible anything else bad in the code (just started python).

``````def chooseRandomNumbers(numbersToChoose, startFrom, stopAt):

numbers = [(randrange(startFrom, stopAt))]  # initialize empty list of numbers

for n in range(numbersToChoose-1):  # choose random number from 1 to 49 a total of 7 times
x = randrange(startFrom, stopAt+1)
for a in numbers:  # check for already existing elements in "numbers"
if x == numbers[a]:  # if new random number already exists in "numbers" discard and run loop again
n -= 1  # decreases n by 1 so a new (hopefully not already existing) number can be generated
else:  # generated numbers does not exist in "numbers" yet
numbers.append(x)  # appends randomly chosen number to list "numbers"

return numbers  # returns said list "numbers"
``````

Any advice on how to deal with this is greatly appreciated. Also please tell me if possible anything else bad in the code (just started python).

Your code to check if generated number is already in the list is wrong. `for a in numbers`, in this loop, you are using `numbers[a]` while `a` is a member of list but not the index of the member. use `in` to test if a number is in list:

from random import randrange

``````def chooseRandomNumbers(numbersToChoose, startFrom, stopAt):
numbers = []
for n in range(numbersToChoose):
x = randrange(startFrom, stopAt+1)
if not x in numbers:
numbers.append(x)
else:
n -= 1
return numbers
``````

or simply:

``````from random import sample
def chooseRandomNumbers(numbersToChoose, startFrom, stopAt):
return sample(range(startFrom,stopAt+1),numbersToChoose)
``````

Your code to check if generated number is already in the list is wrong. `for a in numbers`, in this loop, you are using `numbers[a]` while `a` is a member of list but not the index of the member. use `in` to test if a number is in list:

``````def chooseRandomNumbers(numbersToChoose, startFrom, stopAt):
numbers = []
for n in range(numbersToChoose):
x = randrange(startFrom, stopAt+1)
if not x in numbers:
numbers.append(x)
else:
n -= 1
return numbers
``````

``````from random import sample
def chooseRandomNumbers(numbersToChoose, startFrom, stopAt):
return sample(range(startFrom,stopAt+1),numbersToChoose)
``````
python  python-3.x