Python Random Module (get random number) common methods and use examples _python

Source: Internet
Author: User
Tags function prototype shuffle in python

Random.random
Random.random () is used to generate a 0 to 1 number of random character points: 0 <= N < 1.0

Random.uniform
Random.uniform (A, b), which generates a number of random characters within a specified range, one of two parameters is the upper bound and one is the lower bound. If a > B, the generated random number n:a <= n <= B. If a <b, then B <= N <= a

Copy Code code as follows:

Print Random.uniform (10, 20)
Print Random.uniform (20, 10)
# 18.7356606526
# 12.5798298022

Random.randint
Random.randint (A, b), which is used to generate an integer within a specified range. Where parameter A is the lower bound, parameter B is the upper bound, the generated random number n:a <= n <= B

Copy Code code as follows:

Print Random.randint (12, 20) # generates random numbers N:12 <= n <= 20
Print Random.randint (20, 20) # The result will always be 20
# Print Random.randint (20, 10) # The statement is wrong. Lower limit must be less than upper limit

Random.randrange
Random.randrange ([start], stop[, step] gets a random number from the specified range, in the set incremented by the specified cardinality. such as: Random.randrange (10, 100, 2), the result is equivalent from [10, 12, 14, 16, ... 96, 98] sequence to obtain a random number. Random.randrange (10, 100, 2) is equivalent to Random.choice (range (10, 100, 2) on the result

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 how: sequence is not a specific type in Python, it's a series of types. list, tuple, strings belong to sequence. For sequence, you can view the Python manual data Model chapter. Here are some examples of using choice:

Copy Code code as follows:

Print Random.choice ("Learning python")
Print Random.choice (["Jgood", "is", "a", "handsome", "boy"])
Print Random.choice (("Tuple", "List", "Dict"))

Random.shuffle
Random.shuffle (x[, Random]), which is used to disrupt elements in a list. Such as:

Copy Code code as follows:

p = [' Python ', ' is ', ' powerful ', ' simple ', ' and so on ... ']
Random.shuffle (P)
Print P
# [' Powerful ', ' simple ', ' is ', ' Python ', ' and ' ... ']

Random.sample
Random.sample (sequence, k) that randomly obtains a fragment of the specified length from the specified sequence. The sample function does not modify the original sequence

Copy Code code as follows:

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Slice = random.sample (list, 5) # randomly obtains 5 elements from the list, returning as a fragment
Print Slice
Print List # The original sequence does not change


Random integer:

Copy Code code as follows:
>>> Import Random
>>> Random.randint (0,99)
# 21

Randomly select an even number between 0 and 100:

Copy Code code as follows:
>>> Import Random
>>> random.randrange (0, 101, 2)
# 42

Random floating-point numbers:

Copy Code code as follows:
>>> Import Random
>>> Random.random ()
0.85415370477785668
>>> random.uniform (1, 10)
# 5.4221167969800881

Random characters:

Copy Code code as follows:
>>> Import Random
>>> random.choice (' abcdefg&#%^*f ')
# ' d '

Select a specific number of characters in more than one character:

Copy Code code as follows:
>>> Import Random
Random.sample (' Abcdefghij ', 3)
# [' A ', ' d ', ' B ']

Select a specific number of characters to compose a new string in more than one character:

Copy Code code as follows:
>>> Import Random
>>> Import String
>>> String.Join (Random.sample ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J '], 3)). Replace ("", "")
# ' FIH '

Random Pick String:

Copy Code code as follows:
>>> Import Random
>>> random.choice ([' Apple ', ' pear ', ' peach ', ' orange ', ' Lemon '])
# ' Lemon '

Shuffle:

Copy Code code as follows:
>>> Import Random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle (items)
>>> Items
# [3, 2, 5, 6, 4, 1]

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.