Python Random Module

Source: Internet
Author: User

1 Random.random ()
Used to generate a random number of 0 to 1 points: 0 <= N < 1.0
Print (Random.random ()) #0.6110565855172162

2 Random.randint () #大于等于a且小于等于b之间的整数
The function prototype for Random.randint () is: Random.randint (A, b), which is used to generate integers in a specified range. Where parameter A is the lower limit, parameter B is the upper limit, the generated random number n:a <= n <= B
Print (Random.randint (1,10)) #6

3 Random.randrange () #大于等于a且小于b之间的整数
The function prototype is: Random.randrange ([start], stop[, step]), gets a random number from the specified range, in the collection incremented by the specified cardinality. such as: Random.randrange (10, 100, 2), the result is equivalent from [10, 12, 14, 16, ... 96, 98] gets a random number in the sequence.
Print (Random.randrange (1,10,3)) #4

4 Random.choice
Random.choice gets a random element from the sequence. Its function prototype is: Random.choice (sequence). The parameter sequence represents an ordered type. Here's a note: sequence is not a specific type in Python, but a series of types. list, tuple, string all belong to sequence.

5 Random.uniform ()
The function prototype for Andom.uniform is: Random.uniform (A, b), which is used to generate a random number of points within a specified range of two parameters, one of which is the upper limit and the other is the lower limit. If a > B, the generated random number n:a <= n <= B. If a <b, then B <= N <= A.

6 Random.shuffle ()
The function prototype for random.shuffle is: random.shuffle (x[, Random]), which disrupts the elements in a list
list = [' Python ', ' is ', ' powerful ', ' simple ', ' and so ' on ... ']
Random.shuffle (list)
Print (list) #[' is ', ' and so on ... ', ' simple ', ' powerful ', ' Python ']

#需求, make a 5-bit number and a random number of uppercase and lowercase characters
Import Random
Def code_nu ():
Count = "
For I in range (5):
ADD1 = str (random.randint (1,10))
ADD2 = Chr (Random.randint (65,90))
ADD3 = Chr (Random.randint (97,122))
Add = Random.choice ((ADD1,ADD2,ADD3))
Count = '. Join ([Count,str (add)])
return count

Print (Code_nu ()) #q106Zw

Python Random Module

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.