jeudi 2 février 2017

Why does shuffle generate the same permutation from the second iteration onward in the following code?

I am using shuffle to permute the contents of the lists. The first iteration works as expected and produces a permutation of the original list. However second iteration onward, I get the same permutation being written to the file. Why does the code not produce another permutation in the proceeding iterations?

import random;

f = open('GenerationNumbers.txt', 'w')

request = [ 128,
            512,
            2048,
            512,
            64,
            64,
            512,
            1024,
            1024,
            64,
           ]

#server
server = [ 4096,
           2048,
           2048
         ]

newrequest = []
newserver = []

for i in range(0, 30):
    random.shuffle(request)
    newrequest.append(request)
    random.shuffle(server)
    newserver.append(server)


f.write(str(newrequest)+"\n")
f.write(str(newserver))
f.flush()
f.close()

The following is a part of the output file:

[[1024, 64, 512, 512, 128, 64, 2048, 64, 512, 1024], [1024, 64, 512, 512, 128, 64, 2048, 64, 512, 1024], [1024, 64, 512, 512, 128, 64, 2048, 64, 512, 1024], [1024, 64, 512, 512, 128, 64, 2048, 64, 512, 1024], [1024, 64, 512, 512, 128, 64, 2048, 64, 512, 1024] ....

[[2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], [2048, 4096, 2048], ...




Aucun commentaire:

Enregistrer un commentaire