dimanche 4 octobre 2015

making a list of random numbers - len(50) 1-50 - that doesnt have two numbers together that are within 15 of each other

This is what I have so far but it breaks if I go past plus or minus 11 the previous number. So if the previous number is 1 the next number has to be more than 16. Also Im trying to only use each number once.

Example output:

[3,45, 1, 16, 33, 3, 23.....]

import random
new_array=[]
counter = 0

array=range(51)
array=array[1:51]

while len(new_array)<50:
    y=random.choice(array)
    if y not in new_array and counter!=0 and y not in (range(new_array[counter-1]-11,new_array[counter-1]+11)):
        new_array.append(y)
        counter+=1
    elif counter == 0:
        new_array.append(y)
        counter+=1
    else:
        pass




Aucun commentaire:

Enregistrer un commentaire