"Python standard library Module II" Random module learning

Source: Internet
Author: User

The random module is the module used to generate the stochastic number.
  • Importing the Random module
    Import Random
  • Generates a random number of 0~1, floating-point numbers
    # Randomly generate a random number of 0~1 Print (Random.random ())
  • Generate any range of floating-point numbers
    # randomly take a range of floating-point numbers from the list Print (Random.uniform (1,3))
  • Generate an integer within [x, Y] Range
    # randomly generate a random number Print (Random.randint (1,3))
  • Generates an integer in the [x, y) range, with the head not rounded
    # Randomly generate a random number of one or more Print (Random.randrange (1,3))
  • Randomly select a sequence with multiple elements, such as a list, a tuple, and so on
    # randomly take an element from a list Print (Random.choice ([1,2,3,4]))
  • Randomly select multiple items in a sequence with multiple elements, such as lists, tuples, and so on
    # randomly take 2 elements from a list Print (Random.sample ([1,2,3,4],2)) # printing results for [4, 1]

"Python standard library Module II" Random module learning

Related Article

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.