dimanche 6 mars 2016

Generating random lists in Python query

I want my program to take numbers from 1 to X and randomly distribute those numbers between X/2 numbers of lists Y times. I don't want a number to be repeated during one shuffle, also I don't want the lists to repeat at all. So if there is list [1,2] there shouldn't be another list containing 1 or 2 in same shuffle and there shouldn't be another [1,2] or [2,1] in the whole result.

This is what I came up with, however, it keeps on repeating numbers. Any adice?

  import random

  def Shuffler():
    amount = int(raw_input("Numbers up to: "))
    times = int(raw_input("Number of shuffles: "))
    numberslist = range(1,amount+1)
    twos = []
    thisshuffle = []
    final = []

    while len(final) < (amount/2)*times:
      twos = []
      thisshuffle = []
      while len(twos) < 2:
        if len(numberslist)!=0:
          randomnumber = random.choice(numberslist)
          if (randomnumber in twos) or (randomnumber in thisshuffle):
            numberslist.remove(randomnumber)
          else:
            twos.append(randomnumber)
            thisshuffle.append(randomnumber)
            numberslist.remove(randomnumber)
        else:
          numberslist = range(1,amount+1)

      if (twos or list(reversed(twos))) not in final:
        final.append(twos)

    k=0
    for i in range(times): #this shit prints shit
      print "%s:" % (i+1), final[k:k+amount/2]
      print
      k = k + amount/2
    Shuffler()

  Shuffler()




Aucun commentaire:

Enregistrer un commentaire