找到你要的答案

Q:Unwanted random iterator in For-Loop

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).

我试图生成一组号码创建一个函数(numberstochoose)两个值之间的值(从和STOPAT)但由于某种原因,迭代器(?)在第二个循环(7行),在这种情况下,似乎是随机生成的,即使我没有看到任何原因。

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).

answer1: 回答1:

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:

从随机进口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

或者干脆:

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