1. Random functions in Python

Source: Internet
Author: User
Tags shuffle

This series does not give a detailed description of the Python syntax and theory, so it is not a learning material; A detailed study of the Vamei; is a good (basic-intermediate-Advanced) Learning tutorial. And here is just a summary of some of the topics that I learned about Python. 1. random () function

Description : The random() method returns a randomly generated real number that is within the range of [0,1].

Syntax:
Import randomrandom.random ();
Note: random ()is not directly accessible, you need to import the random module and then call the method through the random static object. Example Demo:
Import Random Print random.random (); Print random.random (); 0.451592468747
2. Randrange () function Description: randrange () method returns a random number in the specified increment cardinality collection, with a cardinality default of 1. Returns an integer

Grammar

Import Randomrandom.randrange ([Start,] stop [, step]) # Parameters:     Start- The starting value within the specified range, contained within the range      - The ending value within the specified range, not included in the range.      --Specify increment cardinality

Example Demo

print Random.randrange (ten); print random.randrange (5,10); print random.randrange (5,10,3); print random.randrange (5,10,3); 8
3.randint () function Description: Randint ()The Randrange method randomly generates an integer that is within the [x, Y] range and is somewhat equivalent to the x,y+1. Grammar
Import randomrandom.randint (x, y) # Parameters:     X-the start value within the specified range, contained within the range      -the end value within the specified range, contained within the range.

Example Demo

print random.randrange (5,10); print random.randint (5,10); 6
4. Uniform () function Description:uniform () Method will randomly generate the next real number, which is within [x, Y] range. Returns a floating-point number

Grammar:

Import randomrandom.uniform (x, y) # Parameters:     X-the start value within the specified range, contained within the range      -the end value within the specified range, contained within the range.

Example Demo

print random.uniform (5,10); print random.uniform (9,10); 9.95958315062
5. Choice () function Description: Choice ()method returns a list of the random items of a tuple or string.

Grammar

Import randomrandom.choice (x) # Parameters:     X--a kind of list,tuple,strings

Example Demo

print random.choice ('a','be', 5,'E  ')print random.choice ([10,2,6,5,85,'af' ])print random.choice ('I love python') v

6. Sample () function

Description: sample ()The method returns random items from a list, a tuple, or a string, and the return type is a tuple type

Grammar

Import randomrandom.sample (x,n) # Parameters:     X-- a kind     of list,tuple,strings --Returns n random items

Example Demo

>>>PrintRandom.sample ('I love Python', 3)[' ','e','I']>>>PrintRandom.sample ([10,20,50,23,'AB'],3)[50,'AB', 23]>>>PrintRandom.sample (10,20,50,23,'AB'), 3)[50, 20,'AB']
7. Shuffle () function

Description: The Shuffle () method randomly sorts all elements of a sequence. Similar to shuffling

Syntax:
Import randomrandom.shuffle (x) # Parameters:     X-one of the list,tuple; python2.x only supports list types

Example Demo

>>> list=['a','b','C','D','e'];>>>random.shuffle (list);>>>Printlist; ['C','D','a','e','b']

Expansion: reverse the meta-ancestor; the effect of implementing the reverse function

>>> list=['a','b','C','D','e'];>>> list1=list[::-1]>>>Printlist1['e','D','C','b','a']



1. Random functions in Python

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.