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