Random number generating Python

Python is random number generating means for random. Here are some random module is the most commonly used several functions.

 

random.random

Random point values ​​random.random () for generating a 0 to 1: 0 <= n <1.0

random.uniform

  Random.uniform function prototype is: random.uniform (a, b), for generating a random point values ​​within the specified range, two parameters one of which is the upper limit, a lower limit. If a> b, the generated random number n: b <= n <= a. If a <b, then a <= n <= b.

[python]  view plain copy
  1. print random.uniform(10, 20)  
  2. print random.uniform(20, 10)  
  3. # ---- results (the results are not the same on different machines)  
  4. #18.7356606526  
  5. #12.5798298022  

random.randint

  the random.randint () function prototype: random.randint (a, b), for generating an integer within a specified range. Wherein A is a lower limit of the parameter, the parameter b is the upper limit, the generated random number n: a <= n <= b

[python]  view plain copy
  1. the random.randint Print ( 12 is,  20 is)   # generated random number n: 12 <= n <=   20
  2. random.randint Print ( 20,  20)   # result will always be 20  
  3. #print random.randint (20, 10) # This statement is wrong. The lower limit must be less than the upper limit.  

random.randrange

  Random.randrange function prototype is: random.randrange ([start], stop [, step]),, acquires a random number from within the specified range of the specified base set increments. Such as: random.randrange (10, 100, 2), the result is equivalent to obtaining a random number from [10, 12, 14, 16, ... 96, 98] in the sequence. random.randrange (10, 100, 2) equivalent random.choice (range (10, 100, 2) on the results.

random.choice

  random.choice acquiring a random element from the sequence. Which is a function prototype: random.choice (sequence). It represents an ordered sequence parameter type. Here to explain  it: sequence in python is not a specific type, but generally refers to a range of types. list, tuple, strings belong sequence. You can view the relevant sequence python manual data model in this chapter, may also refer to: http://www.17xie.com/read-37422.html  . Here are some examples of the use of choice:

[python]  view plain copy
  1. random.choice Print ( "Learning Python")   
  2. print random.choice(["JGood", "is", "a", "handsome", "boy"])  
  3. print random.choice(("Tuple", "List", "Dict"))  

random.shuffle

  Random.shuffle function prototype is: random.shuffle (x [, random]), for a list of elements disrupted. Such as:

[python]  view plain copy
  1. p = ["Python", "is", "powerful", "simple", "and so on..."]  
  2. random.shuffle(p)  
  3. print p  
  4. # ---- results (results on different machines may not be the same.)  
  5. #['powerful', 'simple', 'is', 'Python', 'and so on...']  

random.sample

  Random.sample function prototype is: random.sample (sequence, k), obtaining random fragments specified length from a specified sequence. sample function does not modify the original sequence.

[python]  view plain copy
  1. list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
  2. = random.sample Slice (list,  . 5)   # 5 acquires a random element from a list, as a return segment  
  3. print slice  
  4. List Print  # original sequence has not changed.  

   The above methods are the most commonly used random module in Python manual also describes other methods. Interested friends can find more information by querying the Python manual.

Guess you like

Origin www.cnblogs.com/ybl20000418/p/11486290.html