Random Modules

What You'll Learn: In this tutorial, you'll discover how to use Python's built-in random module to generate random numbers and perform various random operations. This module includes many useful methods for working with randomness.

Key Functions in the random Module

  1. seed()
    • Description: Initializes the random number generator. Useful if you want to make random numbers predictable for testing purposes.
    • Example:

      python
      import random
      random.seed(42)  # Sets the seed for random number generation
      
  2. getstate()
    • Description: Returns the current internal state of the random number generator. This can be saved and used later to restore the state.
    • Example:

      python
      import random
      state = random.getstate()  # Gets the current state
      
  3. setstate(state)
    • Description: Restores the internal state of the random number generator to a specific state. This allows you to continue generating random numbers from where you left off.
    • Example:

      python
      import random
      random.setstate(state)  # Restores the state
      
  4. getrandbits(k)
    • Description: Returns a number representing k random bits.
    • Example:

      python
      import random
      bits = random.getrandbits(5)  # Generates a random number with 5 bits
      print(bits)
      
  5. randrange(start, stop[, step])
    • Description: Returns a random number between the given range. It can take an optional step parameter to specify the increment.
    • Example:

      python
      import random
      num = random.randrange(1, 10, 2)  # Generates a random number between 1 and 10 with step 2
      print(num)
      
  6. randint(a, b)
    • Description: Returns a random number between a and b (inclusive).
    • Example:

      python
      import random
      num = random.randint(1, 10)  # Generates a random number between 1 and 10
      print(num)
      
  7. choice(seq)
    • Description: Returns a random element from the given sequence.
    • Example:

      python
      import random
      fruit = random.choice(['apple', 'banana', 'cherry'])  # Chooses a random fruit from the list
      print(fruit)
      
  8. choices(population, weights=None, *, cum_weights=None, k=1)
    • Description: Returns a list with a random selection from the given sequence.
    • Example:

      python
      import random
      x = random.choices(['apple', 'banana', 'cherry'], k=2)
      print(x)
      
  9. shuffle(x[, random])
    • Description: Takes a sequence and returns the sequence in a random order.
    • Example:

      python
      import random
      my_list = [1, 2, 3, 4, 5]
      random.shuffle(my_list)
      print(my_list)
      
  10. sample(population, k)
    • Description: Returns a given sample of a sequence.
    • Example:

      python
      import random
      x = random.sample(['apple', 'banana', 'cherry'], k=2)
      print(x)
      
  11. random()
    • Description: Returns a random float number between 0 and 1.
    • Example:

      python
      import random
      x = random.random()
      print(x)
      
  12. uniform(a, b)
    • Description: Returns a random float number between two given parameters.
    • Example:

      python
      import random
      x = random.uniform(1, 10)
      print(x)
      
  13. triangular(low, high, mode)
    • Description: Returns a random float number between low and high, with a specified mode parameter for the midpoint.

Try It Yourself: Fun Exercises

  1. Generate Random Numbers:
    • Use randint() to generate random numbers for a simple dice-rolling game.
  2. Randomly Select Items:
    • Use choice() and sample() to randomly select items from a list.
  3. Shuffle a List:
    • Use shuffle() to randomize the order of elements in a list.

Summary:

In this Python tutorial, we learned how to use some basic functions from the random module to generate random numbers and make random choices. These functions are handy for adding randomness to your programs. Keep experimenting and have fun with Python!