0

I'm trying to create a function that, one by one, generates 8 random numbers from 1 to 8 and adds them to an array, checking if they are unique each time before adding them. However, it doesn't work as expected and, while it creates an array consisting of 8 elements, the numbers are not all unique. My code:

import random #Allows the program to generate random numbers

correctSequence = [] #Defines array

def generateSequence(correctSequence): #Defines function, passes array as parameter
    selection = random.randint(1,8) #Creates a random number and adds it to the array so there is a starting point for the for loop (Ln 10)
    correctSequence.append(str(selection))
    while len(correctSequence) < 8: #The while loop will continue to run until the array consists of 8 objects
        selection = random.randint(1,8) #Generates a random number
        for i in range(len(correctSequence)): #Loops through each value in the array

           if correctSequence[i] == selection: #This line checks to see if the value already exists in the array
            print("Duplicate") #This line is meant to print "Duplicate" when a duplicate value is generated

           else:
            correctSequence.append(str(selection)) #If the value doesnt already exist in the array, it will be added
            print("Valid") #This line is meant to print "Valid" when a unique value is generated and added to the array

return correctSequence


#Main body of program

generateSequence(correctSequence) #The function is called
print(correctSequence) #The array is printed

I think the problem occurs somewhere around line 10 as the program seems to go straight to the else statement but I can't see why this would happen.

Additionally, when I run the program, the array, when printed, always seems to repeat the same 2 or 3 numbers multiple times, I don't know if this relates to the already existing issue but it could help to explain what's going on.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 3
    How about `random.sample([1,2,3,4,5,6,7,8], 8)`? – jarmod Dec 28 '19 at 20:14
  • 1
    It cannot work because you append the _string_ version of the number to the list and then check against whether an integer is in the list. Why are you calling `str()` on your numbers when you append them? – roganjosh Dec 28 '19 at 20:14
  • 1
    Or `random.sample(range(1, 9), 8)`. Also note that Python doesn't have arrays; these are lists. – ggorlen Dec 28 '19 at 20:19
  • @ggorlen these are lists, but Python does have arrays – DeepSpace Dec 28 '19 at 20:23
  • @ggorlen https://docs.python.org/3/library/array.html – roganjosh Dec 28 '19 at 20:26
  • Oh. Never used 'em but nice to know. Thanks. Looks like they're [slower than lists](https://stackoverflow.com/questions/176011/python-list-vs-array-when-to-use), so not sure why one would need this with numpy. – ggorlen Dec 28 '19 at 20:26
  • @ggorlen https://docs.python.org/3/library/array.html – DeepSpace Dec 28 '19 at 20:26

2 Answers2

3

8 random numbers from 1 to 8 and adds them to an array, checking if they are unique each time before adding them

There is nothing "random" here. All you need to do is to shuffle the numbers 1 to 8.

import random

nums = list(range(1, 9))
random.shuffle(nums)
print(nums)
# [2, 6, 4, 3, 1, 7, 8, 5]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
-1

For the way you want to do it:

import random #Allows the program to generate random numbers

correctSequence = [] #Defines array

def generateSequence(correctSequence): #Defines function, passes array as parameter
    while len(correctSequence) < 8:
        selection = random.randint(1,8) #Creates a random number and adds it to the array so there is a starting point for the for loop (Ln 10)
        correctSequence.append(selection) if selection not in correctSequence else None
    return correctSequence

generateSequence(correctSequence) #The function is called
print(correctSequence) #The array is printed

But there are better ways for example:

import random #Allows the program to generate random numbers

def generateSequence():
    return random.sample([1,2,3,4,5,6,7,8], 8)
print(generateSequence())

or change the return to return random.sample(range(1, 9), 8) as mentioned above for extra conciseness.

BpY
  • 403
  • 5
  • 12
  • 2
    `correctSequence.append(selection) if selection not in correctSequence else None` is just a piece of ugliness for `if selection not in correctSequence: correctSequence.append(selection)`... – Thierry Lathuille Dec 28 '19 at 20:31
  • Whatever floats your boat, don't understand the down vote though, I answered his question and provided a better way – BpY Dec 28 '19 at 20:32