Bird
Raised Fist0
Pythonprogramming~10 mins

Random data generation in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Random data generation
Import random module
Call random function
Generate random value
Use or print value
Repeat or stop
The program imports the random module, calls a random function to get a value, then uses or prints it, repeating as needed.
Execution Sample
Python
import random

value = random.randint(1, 10)
print(value)
This code generates a random integer between 1 and 10 and prints it.
Execution Table
StepActionFunction CallResultOutput
1Import random moduleN/Arandom module ready
2Call random.randint(1, 10)random.randint(1, 10)7 (example)
3Assign value = 7N/Avalue = 7
4Print valueprint(value)N/A7
5End of programN/AProgram stops
💡 Program ends after printing the random number.
Variable Tracker
VariableStartAfter Step 3Final
valueundefined7 (example)7 (example)
Key Moments - 3 Insights
Why does the random number change every time I run the program?
Because random.randint generates a new random number each time it is called, as shown in execution_table step 2.
Can the random number be outside the range 1 to 10?
No, random.randint(1, 10) always returns a number between 1 and 10 inclusive, as shown in execution_table step 2.
What happens if I forget to import random?
The program will give an error because random module functions won't be recognized, shown by the importance of step 1 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'value' after step 3?
Aundefined
B7 (example)
Crandom module
Dprint output
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step does the program print the random number?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the 'Print value' action in the execution_table.
If you change random.randint(1, 10) to random.randint(5, 15), what changes in the execution_table?
AThe import step changes
BThe variable name changes
CThe printed number range changes to between 5 and 15
DThe program stops earlier
💡 Hint
Focus on the 'Function Call' and 'Result' columns in step 2.
Concept Snapshot
Random data generation in Python:
- Import random module
- Use random.randint(a, b) for random int between a and b
- Each call gives a new random value
- Use print() to show the value
- Always import random before using
Full Transcript
This example shows how Python generates random numbers. First, the random module is imported. Then random.randint(1, 10) is called to get a random integer between 1 and 10. This value is stored in the variable 'value'. Finally, the value is printed. Each time the program runs, a new random number is generated. If the random module is not imported, the program will not work. Changing the range in randint changes the possible random numbers.

Practice

(1/5)
1. What does the random.randint(a, b) function do in Python?
easy
A. Returns a random float between a and b
B. Returns a random integer N such that a ≤ N ≤ b
C. Returns a random element from a list
D. Shuffles the elements of a list in place

Solution

  1. Step 1: Understand the function purpose

    random.randint(a, b) generates a random integer between two given numbers a and b, inclusive.
  2. Step 2: Compare options with function behavior

    Returns a random integer N such that a ≤ N ≤ b correctly describes this behavior. Options A, C, and D describe other functions like random.uniform, random.choice, and random.shuffle.
  3. Final Answer:

    Returns a random integer N such that a ≤ N ≤ b -> Option B
  4. Quick Check:

    random.randint = random integer in range [OK]
Hint: randint returns integers between two numbers inclusive [OK]
Common Mistakes:
  • Confusing randint with random float functions
  • Thinking randint returns a list element
  • Mixing up randint with shuffle
2. Which of the following is the correct way to import the random module and use choice to pick a random element from a list items?
easy
A. import random; random.choice(items)
B. from random import randint; choice(items)
C. import random.choice; choice(items)
D. import random; random.randint(items)

Solution

  1. Step 1: Check import syntax

    To use choice, you must import the random module fully or import choice specifically. import random; random.choice(items) imports the module correctly.
  2. Step 2: Verify function usage

    import random; random.choice(items) calls random.choice(items), which is correct. from random import randint; choice(items) imports randint but tries to call choice without import. import random.choice; choice(items) has invalid import syntax. import random; random.randint(items) calls randint with a list, which is incorrect.
  3. Final Answer:

    import random; random.choice(items) -> Option A
  4. Quick Check:

    Correct import and call = import random; random.choice(items) [OK]
Hint: Import random module fully to use choice function [OK]
Common Mistakes:
  • Importing wrong functions
  • Calling functions without module prefix
  • Using randint instead of choice
3. What is the output of this code?
import random
items = ['apple', 'banana', 'cherry']
random.shuffle(items)
print(items)
medium
A. SyntaxError because shuffle returns a new list
B. ['apple', 'banana', 'cherry'] (always same order)
C. A new list with one random item from items
D. A randomly shuffled list of the original items

Solution

  1. Step 1: Understand random.shuffle behavior

    random.shuffle rearranges the list elements in place randomly. It does not return a new list.
  2. Step 2: Analyze the print output

    After shuffling, printing items shows the same list but with elements in random order. So output is a shuffled list, not the original order or a single item.
  3. Final Answer:

    A randomly shuffled list of the original items -> Option D
  4. Quick Check:

    shuffle changes list order in place [OK]
Hint: shuffle changes list order in place, no new list returned [OK]
Common Mistakes:
  • Expecting shuffle to return a new list
  • Thinking shuffle picks one random item
  • Assuming list order stays same
4. The following code tries to pick a random element from a list but causes an error. What is the problem?
import random
items = ['red', 'green', 'blue']
print(random.choice(items, 1))
medium
A. random.choice needs the list to be converted to a tuple
B. random.choice requires the list to be sorted first
C. random.choice does not take two arguments
D. random.choice only works with strings, not lists

Solution

  1. Step 1: Check random.choice function signature

    random.choice takes exactly one argument: a sequence (like a list). It returns one random element.
  2. Step 2: Identify the error cause

    The code passes two arguments (items and 1), which is invalid and causes a TypeError.
  3. Final Answer:

    random.choice does not take two arguments -> Option C
  4. Quick Check:

    choice takes one argument only [OK]
Hint: choice takes only one argument: the sequence [OK]
Common Mistakes:
  • Passing extra arguments to choice
  • Thinking choice returns multiple items
  • Confusing choice with sample
5. You want to generate a dictionary where keys are numbers from 1 to 5 and values are random integers between 10 and 20. Which code correctly does this?
hard
A. import random result = {i: random.randint(10, 20) for i in range(1, 6)}
B. import random result = {random.randint(10, 20): i for i in range(1, 6)}
C. import random result = {i: random.choice(range(10, 20)) for i in range(1, 6)}
D. import random result = dict(random.randint(10, 20) for i in range(1, 6))

Solution

  1. Step 1: Understand dictionary comprehension syntax

    We want keys as numbers 1 to 5 and values as random integers between 10 and 20. The syntax is {key: value for key in iterable}.
  2. Step 2: Check each option

    import random result = {i: random.randint(10, 20) for i in range(1, 6)} correctly uses i as key and random.randint(10, 20) as value for each i in 1 to 5.
    import random result = {random.randint(10, 20): i for i in range(1, 6)} swaps keys and values incorrectly.
    import random result = {i: random.choice(range(10, 20)) for i in range(1, 6)} uses random.choice(range(10, 20)) which produces integers 10-19 excluding 20, unlike randint(10,20).
    import random result = dict(random.randint(10, 20) for i in range(1, 6)) tries to convert a generator of integers to dict, which causes an error.
  3. Final Answer:

    import random result = {i: random.randint(10, 20) for i in range(1, 6)} -> Option A
  4. Quick Check:

    Correct dict comprehension with randint [OK]
Hint: Use dict comprehension with randint for random values [OK]
Common Mistakes:
  • Swapping keys and values
  • Using dict() on generator of ints
  • Using choice with range(10,20) excludes 20