mardi 2 août 2022

Issues with random.randint

So I'm generating 4 random lists and then plugging them into a function called johnson. When I simply generate the 4 random lists everything looks fine. But if I try to plug in the result into the function johnson, the "randomness" seems to disappear. I'm not sure what's going on. Any help would be appreciated.


import random

def generate_Lt_Ut(n, delta):
    Ut1 = [random.randint(1,100) for i in range(n)]
    Ut2 = [random.randint(1,100) for i in range(n)]
    Lt1 = [random.randint(max(1,Ut1[i]-delta),Ut1[i]) for i in range(n)]
    Lt2 = [random.randint(max(1,Ut2[i]-delta),Ut2[i]) for i in range(n)]
    return Ut1, Ut2, Lt1, Lt2

def johnson(t1, t2):
    n = len(t1)
    #each time we find a minimal number, we want to cross out that job, so
    #we replace it with a number greater than all numbers in t1 and t2 combined.
    maximum1 = max(t1)
    maximum2 = max(t2)
    maxplus1 = max(maximum1, maximum2) + 1
    #seq1 is the sequence that will contain jobs from machine 1 and seq2 will
    #contain jobs from machine 2. At the end, we will reverse the sequence
    #seq2 and combine seq1 and seq2 to obtain the desired sequence.
    seq1 = []
    seq2 = []
    while len(seq1)+len(seq2)< len(t1):
        mt1 = min(t1)
        mt2 = min(t2)
        if mt1 == mt2 or mt1 < mt2:
            i = t1.index(mt1)
            seq1.append(i+1)
            t1[i] = maxplus1
            t2[i] = maxplus1
        elif mt1 > mt2:
            i = t2.index(mt2)
            seq2.append(i+1)
            t1[i] = maxplus1
            t2[i] = maxplus1
    return (seq1 + seq2[::-1])

Ut1, Ut2, Lt1, Lt2 = generate_Lt_Ut(4, 30)
u = johnson(Lt1, Lt2)
v = johnson(Ut1, Ut2)

print("Ut1: " + str(Ut1))
print("Lt1: " + str(Lt1))
print("Ut2: " + str(Ut2))
print("Lt2: " + str(Lt2))
print("u " + str(u))
print('u ' + str(v))

This is the output:

Ut1: [100, 100, 100, 100]
Lt1: [75, 75, 75, 75]
Ut2: [100, 100, 100, 100]
Lt2: [75, 75, 75, 75]
u [1, 3, 4, 2]
u [1, 4, 2, 3]

As you can see, the 4 lists Ut1, Lt1, Ut2, Lt2 don't look random at all. But if I don't plug them into johnson, then the lists do look random. This is very confusing to me...why is this happening?




Aucun commentaire:

Enregistrer un commentaire