I have a line of code where can sort an array into random order:
someArray.sort_by {rand}
so in python, how can I convert to it to python code?
CodePudding user response:
Maybe you are looking for this:
import random
l = [1, 2, 3]
# l is shuffled in place
random.shuffle(l)
# Print to see the shuffled l
print(l)
CodePudding user response:
After reading the comments of @Aidis answer, it looks like that you want a cryptographically secure version of random.shuffle
. That's what I came up with using urandom
:
import os
array = [1,2,3,4,5,6,7,8,9]
array = sorted(array, key=lambda f: int.from_bytes(os.urandom(8), 'big'))
print(array)