Learn Python by yourself: generate random sequences from 1 to 1000

Our general sequences are either arranged in order such as 1, 2, 3, 4, or in reverse order, such as 10, 9, 8, 7, 6.

So how do you generate a sequence of random numbers from 1 to 1000?

It's easy to do with Python.

The statement is as follows:

import random #Import random module

a=[x for x in range(1,51)] #This only demonstrates the sequence from 1 to 50. If you want more, you only need to modify the value of the next 51

print(a)

random.shuffle(a) #shuffle the order into a random arrangement

print(a)

The execution result is as follows:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

[5, 1, 3, 46, 15, 14, 30, 35, 43, 41, 17, 23, 38, 25, 31, 44, 29, 34, 9, 50, 12, 40, 8, 33, 21, 47, 49, 16, 24, 11, 42, 28, 4, 27, 6, 7, 26, 45, 20, 36, 2, 13, 18, 37, 39, 32, 19, 22, 48, 10]

————————————————

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801116&siteId=291194637