Pythonrandom module (obtain random number) common methods and examples

Source: Internet
Author: User
Tags shuffle
This article mainly introduces the common methods and examples of using the Pythonrandom module (to obtain random numbers). For more information, see Random. random
Random. random () is used to generate a random number of points 0 to 1: 0 <= n <1.0

Random. uniform
Random. uniform (a, B) is used to generate a random number of characters in a specified range. one of the two parameters is the upper limit and the other is the lower limit. If a> B, the random number n: a <= n <= B. If

The code is as follows:


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

Random. randint
Random. randint (a, B) is used to generate an integer in the specified range. Where parameter a is the lower limit, parameter B is the upper limit, and the generated random number n: a <= n <= B

The code is as follows:


Print random. randint (12, 20) # generated random number n: 12 <= n <= 20
Print random. randint (20, 20) # The result is always 20
# Print random. randint (20, 10) # This statement is incorrect. The lower limit must be less than the upper limit.

Random. randrange
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 ).

Random. choice
Random. choice gets a random element from the sequence. The function prototype is 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. For sequence, see The python manual data model chapter. The following are examples of using choice:

The code is 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]) is used to disrupt the elements in a list. For example:

The code is as follows:


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

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

The code is as follows:


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 them as a piece.
Print slice
Print list # the original sequence has not changed


Random integer:

The code is as follows:

>>> Import random
>>> Random. randint (0, 99)
#21

Randomly select an even number between 0 and 100:

The code is as follows:

>>> Import random
>>> Random. randrange (0,101, 2)
#42

Random floating point number:

The code is as follows:

>>> Import random
>>> Random. random ()
0.85415370477785668
>>> Random. uniform (1, 10)
#5.4221167969800881

Random characters:

The code is as follows:

>>> Import random
>>> Random. choice ('abcdefg & # % ^ * f ')
# 'D'

Select a specific number of characters from multiple characters:

The code is as follows:

>>> Import random
Random. sample ('abcdefghj', 3)
# ['A', 'D', 'B']

Select a specific number of characters to form a new string:

The code is as follows:

>>> Import random
>>> Import string
>>> String. join (random. sample (['A', 'B', 'C', 'D', 'e', 'e', 'F', 'G', 'H', 'I ', 'j'], 3 )). replace ("","")
# 'Fih'

Randomly selected string:

The code is as follows:

>>> Import random
>>> Random. choice (['apple', 'pear ', 'peach', 'Orange ', 'lemon'])
# 'Limon'

Shuffling:

The code is as follows:

>>> Import random
>>> Items = [1, 2, 3, 4, 5, 6]
>>> Random. shuffle (items)
>>> Items
# [3, 2, 5, 6, 4, 1]

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.