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.
- print random.uniform(10, 20)
- print random.uniform(20, 10)
- # ---- results (the results are not the same on different machines)
- #18.7356606526
- #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
- the random.randint Print ( 12 is, 20 is) # generated random number n: 12 <= n <= 20
- random.randint Print ( 20, 20) # result will always be 20
- #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:
- random.choice Print ( "Learning Python")
- print random.choice(["JGood", "is", "a", "handsome", "boy"])
- 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:
- p = ["Python", "is", "powerful", "simple", "and so on..."]
- random.shuffle(p)
- print p
- # ---- results (results on different machines may not be the same.)
- #['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.
- list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- = random.sample Slice (list, . 5) # 5 acquires a random element from a list, as a return segment
- print slice
- 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.