Random Module

2 min read ·

The random module is used to generate random values. It is widely used in games, simulations, testing, and real world applications.

Random Numbers Generation

This generates a random float number between 0 and 1.

randint()

Generates a random integer within a given range.

choice()

Selects a random element from a list.

shuffle()

Shuffles elements of a list randomly.

Note

shuffle modifies the original list and does not return a new list.


Real world Use Cases

Simple Game Example


Simulation Example


Pro Tip

Random module is useful for generating test data and building interactive programs.


Combining math and random


Important Points

  • random generates unpredictable values
  • Useful in games and simulations
  • Can be combined with math for calculations

Caution

Random values are pseudo random and not suitable for secure applications.


Exercise

Generate random number between 1 and 100 Pick random item from a list Shuffle a list of numbers Calculate area using random radius
Learn Python Random Module | Python Course