random — Get Random Number Module

random The main functionality and functions of the module



Used to generate a random number of characters from 0 to 1:0 <= n < 1.0

random.uniform(a, b)

Used to generate a random number of characters within a specified range, with two parameters, one upper and one lower.If a > b, then the generated random number n: a <= n <= b.If a <b, then b <= n <= a.Parameters:

  • a - Upper/lower limit
  • b - Upper/lower limit

The reference code is as follows:

print random.uniform(10, 20)
print random.uniform(20, 10)
# 18.7356606526
# 12.5798298022
random.randint(a, b)

Used to generate an integer within a specified range.Where parameter “a” is the lower limit and parameter “b” is the upper limit, the random number n generated is: a <= n <= b.

  • a - Lower limit
  • b - Upper limit

The reference code is as follows:

print random.randint(12, 20)
print random.randint(20, 20)
# print random.randint(20, 10)
random.randrange([start, ]stop[, step])

Gets a random number from a collection that is incremented by the specified cardinality within the specified range. Such as: random.randrange(10, 100, 2), the result is equivalent to obtaining a random number from the [10, 12, 14, 16… 96, 98] sequence.

  • start - Specified cardinality
  • stop - Upper limit
  • step - Increasing unit

Gets a random element from a sequence. Parameters:

  • sequence - Represents an ordered type.

The reference code is as follows:

print random.choice("Study Python")
print random.choice(["JGood", "is", "a", "handsome", "boy"])
print random.choice(("Tuple", "List", "Dict"))
random.shuffle(x[, random])

Used to scramble elements in a list, parameters:

  • x - Need a shuffled list.

The reference code is as follows:

p = ["Python", "is", "powerful", "simple", "and so on..."]
print p
# ['powerful', 'simple', 'is', 'Python', 'and so on...']
random.sample(sequence, k)

Gets a random fragment of a specified length from a specified sequence, and the sample function does not modify the original sequence.Parameters:

  • sequence - Sequence
  • k - Fragment length

The reference code is as follows:

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
slice = random.sample(list, 5)
print slice
print list

Sample Code:

import time
import random

while True:
    x = int(random.randint(200, 600))
    print("x is:", x)