mercredi 6 novembre 2019

python randomizing troubles

I've been trying to make a script to randomize the positions in a group of numbers. EX: I'll give it the numbers 1, 2, 3, 4 and it will spit back 3214, 2341, 4132, etc. My problem is that after a few iterations it will start giving me output such as 1421, 3421, 2421. Here is my code,

def Solution():
    global A, B, C, D, SOLVED, SOL, A1, B1, C1, D1
    Attempts = []
    Orderer = []
    A1 = 0
    B1 = 0
    C1 = 0
    D1 = 0
    while SOLVED != True:
        MASTER = ""
        A1 = randint(1, 4)
        B1 = randint(1, 4)
        C1 = randint(1, 4)
        D1 = randint(1, 4)
        while B1 == A1:
            B1 = randint(1, 4)
            #print("B1 is the same")
        while C1 == B1 or C1 == A1:
            C1 = randint(1, 4)
            #print("C1 is the same")
        while D1 == C1 or D1 == B1 or D1 == A1:
            D1 = randint(1, 4)
            #print("D1 is the same")
        for i in range(4):
            if A1 == i:
                Orderer.append(A)
            elif B1 == i:
                Orderer.append(B)
            elif C1 == i:
                Orderer.append(C)
            elif D1 == i:
                Orderer.append(D)
        for i in range(4):
            MASTER = MASTER + str(Orderer[i - 1])
        if MASTER in Attempts:
            print("{} was already guessed".format(MASTER))
            continue
        print(MASTER)

        if MASTER == SOL:
            print("Solution: {}".format(MASTER))
            SOLVED = True
        Attempts.append(MASTER)

        MASTER = ""
        Orderer.clear()

note: Just some clarifications; vars A, B, C, and D are the 1, 2, 3, 4 listed above. I gave the computer a "solution" under SOL so it doesn't infinitely run. In this case, the solution is 1234 Also, I put a check so it doesn't make combinations it already made. It's stored under attempts. A1, B1, C1, and D1 are the position values for A, B, C, and D. They are pumped into the orderer and put then get stored in that order.




Aucun commentaire:

Enregistrer un commentaire