Python randomly generates phone numbers, numbers

Source: Internet
Author: User
Tags shuffle

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

#  -*-coding:gbk-*-

import random


#随机产生26个手机号: Start with 13, followed by an arbitrary number between a 4~9, followed by a 8-digit random number for
_ in range (26):
    print (' + ' +
          str (random.randrange (4,10)) +
          '. Join (STR (random.choice (range)) for _ in range (8))
          )


#随机产生26个数字: Produces a random decimal between a 0~1, multiplied by 1000, rounded to 3 digits after the decimal, plus a number #由于sample函数返回的结果是list类型的 between randomly generated 30~59
, where the 1th value
is fetched through [0] For I in range (num):
    print (Round (random.random () * 1000,3) + random.sample (range (30,60,3), 2) [0])

Introduction to Functions:

(1) Randrange: This function produces a range of any number between 4~9, noting that it does not contain 10.

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

(2) Choice: The data source is a range (10), that is, from the 0~9 randomly select a number, multiple calls can produce duplicate 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: Generating random decimals from 0~1

>>> import Random
>>> random.random ()
0.7379992978183179
>>> random.random ( )
0.4720995823183177
(4) Sample: Data source is range (30,60,3), from 30~59 (step is 3), that is 30, 33, 36 ... In such numbers, select 2, and these 2 numbers will not 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, you can set the same seed

>>> 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]


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.