A function that generates random numbers in Python

Source: Internet
Author: User

Reference Blog: http://www.360doc.com/content/14/0430/11/16044571_373443266.shtml

Today I suddenly think of python how to generate random numbers? Check it out, put out the test results.

Import this module first

1.random.random (): Randomly generates a floating-point number, in the range of 0<=x<=1.0

>>> random.random ()0.7802959818148015>>> random.random ()0.328008839087651 >>> random.random ()0.5568708122526114>>> random.random ()0.23048925282509325

2.random.uniform (): randomly generates random floating-point numbers within the specified range.

>>> random.uniform (18,15)  a >b a<b a=b can be 16.01770569291661>>> random.uniform ( 18,15)17.027730377035027>>> random.uniform (10,15)14.682052726572774>>> Random.uniform (10,15)12.997092389884806

3.random.randint (): Randomly selects integers in a range

>>> random.randint (12,14)12>>> random.randint (12,16)14>>> Random.randint (12,16)16

4.random.randrange (): Similar to Randranint (), returns an integer within the range, and Randrange () can specify the step size of the numeric value.

>>> Random.randrange (10,100)97>>> Random.randrange (10,100)#Step default 189>>> Random.randrange (10,100,2)66>>> Random.randrange (10,100,2)52>>> Random.randrange (10,1,2)#The first number must be less than the second numberTraceback (most recent): File"<stdin>", Line 1,inch<module>File"/usr/local/python3.6/lib/python3.6/random.py", Line 212,inchRandrangeRaiseValueError ("Empty range for Randrange ()") Valueerror:empty Range forRandrange ()

5.random.chiose (): Randomly selects an element in a sequence

>>> Random.choice (('Kebi','Maoxina','Xinye'))'Xinye'>>> >>> Random.choice (('Kebi','Maoxina','Xinye'))'Kebi'>>> Random.choice ([1,2,3,4])2>>> Random.choice ([1,2,3,4])2>>> Random.choice ("old boy It education")'Fertility'>>> Random.choice ("old boy It education")'male'

6.random.shuffle (): Disrupts an element in the list

>>> name = ['Kebi','like','Pig']>>>random.shuffle (name)>>>name['like','Pig','Kebi']>>>random.shuffle (name)>>>name['Kebi','like','Pig']>>>random.shuffle (name)>>>name['like','Pig','Kebi']

7.random.sample (): Randomly gets the specified number of elements in a sequence

>>> SS = (1,2,3,4)>>> SST ='Woshishui'>>> Random.sample (ss,2)[2, 4]>>> Random.sample (ss,2)[2, 1]>>> Random.sample (sst,1)['h']>>> Random.sample (sst,4)#not slices, no order.['o','u','I','W']>>> random.sample (name)#quantity must be specifiedTraceback (most recent): File"<stdin>", Line 1,inch<module>typeerror:sample () missing1 Required positional argument:'k'>>> Random.sample (name,2)['Pig','like']>>> Random.sample (name,1)['Pig']

A function that generates random numbers in Python

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.