Python Random Number

Source: Internet
Author: User

These methods are located in the random module.

Random. Random (), same as the Javascript math. Random (), returns a floating point number between [0.0, 1 ).

Random. Uniform (a, B), return the floating point between [a, B]

 
Print random. Uniform (10, 20) print random. Uniform (20, 10) # ---- result (different results on different machines) #18.7356606526 #12.5798298022

Random. randint (a, B), returns an integer between [a, B]

Print random. randint (12, 20) # generated random number N: 12

Random. randrange ([start], stop [, step]), obtains a random number from the set that increments by the specified base number within the specified range. For example: Random. randrange (10,100, 2), the result is equivalent to obtaining a random number from the [10, 12, 14, 16,... 96, 98] sequence.

Random. randrange (10,100, 2) is equivalent to random. Choice (range (10,100, 2) in the result.

Random. Choice (sequence ). The sequence parameter indicates an ordered type. Sequence is not a specific type in Python, but a series of types. List, tuple, and string all belong to sequence.
Print random. choice ("Learn Python") print random. choice (["jgood", "is", "A", "handsome", "boy"]) print random. choice ("tuple", "list", "dict "))

Random. Shuffle (X [, random]) is used to disrupt the elements in a list.

 
P = ["Python", "is", "powerful", "simple", "and so on... "] Random. shuffle (p) print P # ---- result (the results on different machines may be different .) # ['Powerful', 'simple', 'is', 'python', 'and so on... ']

Random. Sample (sequence, k), random get the specified length from the specified sequence. The sample function does not modify the original sequence.

 
List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Slice = random. sample (list, 5) # randomly obtain 5 elements from the list, and return the print slice print list as a piece # The original sequence has not changed.

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.