# Detailed explanation of Python method for random generation of mobile phone Numbers and Numbers

- 2020-06-12 09:55:14
- OfStack

This paper introduces Python's method of randomly generating mobile phone Numbers and Numbers. To share for your reference, specific as follows:

Python generates mobile phone Numbers and Numbers randomly. The code is as follows:

```
# -*- coding:gbk -*-
import random
# Randomly generated 26 Phone number: Yes 13 First, followed by 1 position 4~9 In between 1 Bit Numbers, followed by 8 Bit random number
for _ in range(26):
print('13' +
str(random.randrange(4,10))+
''.join( str(random.choice(range(10))) for _ in range(8) )
)
# Randomly generated 26 A number: produced 1 a 0~1 The random decimal in between, times 1000,4 Give up 5 When I get to the decimal point 3 Bits, plus random generation 30~59 The number between
# Due to the sample The result of the function is list Student: Of type, through here [0] Take the first 1 A value
for i in range(26):
print( round(random.random()* 1000,3 ) + random.sample(range(30,60,3),2)[0] )
```

Function introduction:

(1) randrange: This function will generate any 1 number between 4 and 9. Note that it does not include 10.

```
>>> random.randrange(5)
2
>>> random.randrange(1,5)
1
```

(2) choice: The data source is range(10), that is, a number is randomly selected from 0 to 9. Repeated calls may produce repeated values

```
>>> random.choice(['a','b','c','d','e'])
'b'
>>> random.choice(['a','b','c','d','e'])
'd'
>>> random.choice(['a','b','c','d','e'])
'c'
>>> random.choice(['a','b','c','d','e'])
'b'
```

(3) random: Produces random decimals from 0 to 1

```
>>> import random
>>> random.random()
0.7379992978183179
>>> random.random()
0.4720995823183177
```

(4) sample: The data source is range(30,60,3), from 30 to 59 (step is 3), that is, 30, 33, 36... Of these Numbers, choose two. They don't repeat

```
>>> random.sample(['a','b','c','d','e'],2)
['d', 'b']
>>> random.sample(['a','b','c','d','e'],2)
['a', 'b']
>>> random.sample(['a','b','c','d','e'],2)
['e', 'd']
```

Other functions:

(5) seed: To return the same random number, the same seed can be set

```
>>> random.seed(5)
>>> random.random()
0.6229016948897019
>>> random.seed(5)
>>> random.random()
0.6229016948897019
```

(6) shuffle: Random arrangement

```
>>> t=[0,1,2,3,4,5,6]
>>> t
[0, 1, 2, 3, 4, 5, 6]
>>> random.shuffle(t)
>>> t
[5, 4, 2, 0, 6, 1, 3]
```

PS: Here are a few more tools for your reference:

Online random generation of personal information data tools:

http://tools.ofstack.com/aideddesign/rnd_userinfo

Online random character/random password generation tool:

http://tools.ofstack.com/aideddesign/rnd_password

Online random number/string generation tool:

http://tools.ofstack.com/aideddesign/suijishu

Frequently used telephone Numbers online enquiry:

http://tools.ofstack.com/bianmin/pub_tel

More about Python related content interested readers to view this site project: "Python mathematical operation skills summary", "Python string skills summary", "Python coding skills summary", "Python data structure and algorithm tutorial", "Python function using skills summary", "introduction to Python and advanced tutorial" and "Python file and directory skills summary"

I hope this article has been helpful in Python programming.