Random in Python

The random module in Python provides functions for generating random numbers. Here's an overview of some commonly used functions within the random module:
  1. 1. random(): Returns a random floating-point number in the range [0.0, 1.0).

    import random value = random.random()
  2. 2. randint(a, b): Returns a random integer in the range [a, b], including both endpoints.

    import random value = random.randint(1, 10)
  3. 3. choice(seq): Returns a random element from the non-empty sequence seq.

    import random value = random.choice([1, 2, 3, 4, 5])
  4. 4. shuffle(seq): Shuffles the elements of a sequence in place.

    import random my_list = [1, 2, 3, 4, 5] random.shuffle(my_list)
  5. 5. sample(population, k): Returns a new list containing k unique elements randomly chosen from the given population.

    import random sample_list = random.sample(range(1, 11), 3)
  6. 6. uniform(a, b): Returns a random floating-point number in the range [a, b) or [b, a) depending on which boundary is smaller.

    import random value = random.uniform(1.5, 2.5)
  7. 7. randrange(start, stop, step): Returns a randomly selected element from the specified range. Similar to range(), you can provide start, stop, and step parameters.

    import random value = random.randrange(1, 10, 2)