Python Learning Summary 7: Random strings and random numbers

Source: Internet
Author: User
Tags function prototype shuffle

Python generates random numbers and random strings, which requires the import of the random module. Some of the most common functions of the random module are as follows:

1. Random.random (A, B)

Used to generate a random number of 0 to 1 points: 0 <= N < 1.0

Import random>>>0.85415370477785668>>> random.uniform (1, ten) 5.4221167969800881

2. random.uniform

The function prototype for Random.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. N:a <= n <= B. If a <b, then B <= N <= A.

Print random.uniform (+)   print random.uniform (ten)   # ----   # 18.7356606526    #

3. Random.randint

Used to generate an integer within a specified range. Where parameter A is the lower limit, parameter B is the upper limit, Python generates a random number, and the lower limit must be less than the upper limit.

print random.randint (# # of  random numbers generated N:12 <= n <=print Random.randint (The   result is always#

4. Random.randrange

The function prototype for Random.randrange is: Random.randrange ([start], stop[, step]), which 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. Random.randrange (10, 100, 2) is equivalent to Random.choice (range (10, 100, 2) on the result.

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

5. Random.choice

Used to generate random characters

>>>ImportRandom>>> Random.choice ('abcdefg&#%^*f')'D‘
# randomly pick a string:

>>> import random

>>> random.choice ([ apple< Span style= "color: #800000;" > ', ' pear ", ' ')

lemon< Span style= "color: #800000;" > '

6. Random.sample

The function prototype for random.sample is: random.sample (sequence, k), which randomly fetches fragments of the specified length from the specified sequence. The sample function does not modify the original sequence.

>>>ImportRandomrandom.sample ('Abcdefghij', 3) ['a','D','b']#Select a specific number of characters to form a new string in multiple characters:
>>>Importstring>>> String.Join (Random.sample (['a','b','C','D','e','F','g','h','I','J'], 3). Replace (" ","")'FIH'

7. Random.shuffle

The function prototype for random.shuffle is: random.shuffle (x[, Random]), which is used to disrupt elements in a list.

 p = [ python  , "  is  , "  powerful  , "   Simple  , "   And so on ...   "  Random.shuffle (p)  print   p  #  ----results (results may differ on different machines.)  #  [' powerful ', ' simple ', ' is ', ' Python ' , ' and so on ... ']  

Python Learning Summary 7: Random strings and random numbers

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.