Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

In practice, we will inevitably use random operations. For example, annual lottery, random selection of winning users; In crawler, user-agent and so on are randomly selected. Today we’ll take a look at random operations in Python.

The random number

randint

The following code implements generating random integers (positive and negative).

import random
res1 = random.randint(-100.100)
print(res1)
Copy the code

Execution Result:

PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
-11
PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
63
Copy the code

randrange

The following code implements random number generation (specifying step size).

import random
res2 = random.randrange(-100.100.2)
print(res2)
Copy the code

Execution Result:

PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
-40
PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
56
Copy the code

Above, you will notice that the generated random numbers are all 2 step numbers between -100 and 100, that is, there will be no odd numbers.

random

Random generates a random number between 0 and 1 of type float.

import random
res3 = random.random()
print(res3)
print(type(res3))
Copy the code

Execution Result:

PS C:\Users\ XXX \Desktop\study> & D:/Python37/python.exe test.py 0.49365074829039834 <class 'float'> PS C:\Users\ XXX \Desktop\study> & D:/Python37/python.exe test.py 0.9803235033128287 <class 'float'>Copy the code

uniform

Uniform returns a random real number of type float in the range.

Import random res4 = random. Uniform (-1.2,9.8) print(res4)Copy the code

Execution Result:

PS C:\Users\ XXX \Desktop\study> & D:/Python37/python.exe test.py 5.61996222607611 PS C:\Users\ XXX \Desktop\study> & D: / Python37 / python. Exe test. Py - 0.8188126541465378Copy the code

Random selection

choice

Choice implements a random selection of an element from the object to be selected.

import random

lst = ['xian'.'beijing'.'shanghai']
res5 = random.choice(lst)
print(res5)
Copy the code

Execution Result:

PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
shanghai
PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
xian
Copy the code

The choice parameter type can be list, tuple, string, etc.

Random sequence

shuffle

Sequence Allows you to randomly sequence a list or string.

import random

lst = ['xian','beijing','shanghai']

random.shuffle(lst)
print(lst)
Copy the code

Execution Result:

PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
['beijing', 'xian', 'shanghai']
PS C:\Users\xxx\Desktop\study> & D:/Python37/python.exe test.py
['shanghai', 'beijing', 'xian']
Copy the code

Note: The shuffle method does not return a value and applies directly to the original object.

Write in the last

With regard to the seed method, it is actually a constructor for the random.Random class instantiation that reseeds before each selection to get a repeatable stream of random numbers.

That’s all for today, thank you for reading, like to remember the three lian oh.