0

I'm trying to create an array of 8 integers, all random and unique e.g. {2,1,3,4,8,5,6,7} and using this I want to create a crossover function for the 8-Queen Problem. Once I have an index and backtrack to check the previous values, the random function fails...

==>Heres a bit of code to have an abstract idea........

import random

arr=[0 for i in range(8)]

arr[0]=random.randint(1,8)
for x in range(1,8):
    for y in range(x,-1,-1):
        if(arr[x]==arr[y]):
            arr[x]=random.randint(1,8)

for q in arr:
        print(q)
tfw
  • 353
  • 1
  • 2
  • 13
  • 3
    `arr= list(range(1,9))` followed by `random.shuffle(arr)` ? or are you looking for something else? – John Coleman Apr 12 '19 at 18:55
  • you can use set data structure which stores unique values – AviatorX Apr 12 '19 at 18:57
  • Is that the 8 queens on the chessboard puzzle? – accdias Apr 12 '19 at 18:58
  • @JohnColeman You have made it very simple, I wasn't thinking of it this way. Much appreciated. –  Apr 12 '19 at 19:23
  • Possible duplicate of [Shuffle an array with python, randomize array item order with python](https://stackoverflow.com/questions/473973/shuffle-an-array-with-python-randomize-array-item-order-with-python) – John Coleman Apr 12 '19 at 21:00

0 Answers0