Python Random Module Common methods _python

Source: Internet
Author: User

Copy Code code as follows:

Import Random
Print Random.random ()

Gets a floating-point number less than 1

Copy Code code as follows:

Import Random
Random.randint (1,10)

Gets an integer from 1 to 10

Copy Code code as follows:

Import Random
Print Random.uniform (0,2)

Gets a floating-point number that is greater than 0 or less than 2

Copy Code code as follows:

Import Random
Print Random.randrange (1,10,4)

Gets a random number from 1 to 10 steps to 4

Copy Code code as follows:

Import Random
a=[1,2,3,4,5]
Random.choice (a)

Remove an element from List a randomly

Copy Code code as follows:

Import Random
a=[1,2,3,4,5]
Random.shuffle (a)

Disrupt the order of elements in List A

Copy Code code as follows:

Import Random
a=[1,2,3,4,5]
Random.sample (a,3)

Remove 3 elements from List A in random order (one element can only be fetched once, so the number of fetched cannot be greater than the number of elements contained in the list)

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.