Python Random Number Module code details

Source: Internet
Author: User

The following articles mainly introduce the Python Random Number Module modules through the code of the Python Random Number Module. If you understand these modules, it will be better used in the practical application of the Python random number module. The following is a detailed introduction of the article.

Random INTEGER:

 
 
  1. >>> import random  
  2. >>> random.randint(0,99)  
  3. 21 

Randomly select an even number between 0 and 100:

 
 
  1. >>> import random  
  2. >>> random.randrange(0, 101, 2)  
  3. 42 

Random Floating Point Number:

 
 
  1. >>> import random  
  2. >>> random.random()   
  3. 0.85415370477785668  
  4. >>> random.uniform(1, 10)  
  5. 5.4221167969800881 

Random characters:

 
 
  1. >>> import random  
  2. >>> random.choice('abcdefg&#%^*f')  
  3. 'd' 

Select a specific number of characters from multiple characters:

 
 
  1. >>> import random  
  2. random.sample('abcdefghij',3)   
  3. ['a', 'd', 'b'] 

In the Python Random Number Module, select a specific number of characters to form a new string:

 
 
  1. >>> import random  
  2. >>> import string  
  3. >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r  
  4. eplace(" ","")  
  5. 'fih' 

Randomly selected string:

 
 
  1. >>> import random  
  2. >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )  
  3. 'lemon' 

Shuffling:

 
 
  1. >>> import random  
  2. >>> items = [1, 2, 3, 4, 5, 6]  
  3. >>> random.shuffle(items)  
  4. >>> items  
  5. [3, 2, 5, 6, 4, 1]  

The above article introduces the Python Random Number Module.

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.