How to randomize a sorted list of numbers in Python

3 Answers

0 votes
import random

sorted_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

randomized_list = random.sample(sorted_list, len(sorted_list))

print(randomized_list)


       
'''
run:

[6, 5, 4, 1, 3, 9, 7, 10, 2, 8]
   
'''

 



answered Apr 18 by avibootz
0 votes
import random

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

random.shuffle(lst)

print(lst)


       
'''
run:

[3, 5, 10, 1, 6, 2, 9, 7, 8, 4]
   
'''

 



answered Apr 18 by avibootz
0 votes
import numpy as np

sorted_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

randomized_list = np.random.permutation(sorted_list).tolist()

print(randomized_list)


       
'''
run:

[6, 7, 8, 3, 4, 5, 9, 1, 10, 2]
   
'''

 



answered Apr 18 by avibootz
...