Random generation helps us create data that looks natural and unpredictable. It is useful to test ideas and make decisions when we don't have real data.
Why random generation matters in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
import numpy as np # Generate a random number between 0 and 1 random_number = np.random.rand() # Generate random integers between low (inclusive) and high (exclusive) random_int = np.random.randint(low, high, size=size) # Generate random numbers from a normal distribution random_normal = np.random.randn(size)
np.random.rand() creates random floats between 0 and 1.
np.random.randint() creates random integers in a range you choose.
Examples
NumPy
import numpy as np # One random float between 0 and 1 print(np.random.rand())
NumPy
import numpy as np # Five random integers from 0 to 9 print(np.random.randint(0, 10, 5))
NumPy
import numpy as np # Three random numbers from a normal distribution print(np.random.randn(3))
Sample Program
This program simulates rolling a die 10 times and shows the results and their average.
NumPy
import numpy as np # Simulate rolling a six-sided die 10 times rolls = np.random.randint(1, 7, 10) print("Rolls:", rolls) # Calculate the average roll average_roll = np.mean(rolls) print(f"Average roll: {average_roll:.2f}")
Important Notes
Random numbers are not truly random but are good enough for most uses.
Setting a seed with np.random.seed() makes results repeatable for testing.
Summary
Random generation helps create unpredictable data for testing and simulations.
NumPy provides easy functions to generate random numbers in different ways.
Using random data can improve experiments, games, and machine learning models.
Practice
1. Why is random number generation important in data science?
easy
Solution
Step 1: Understand the role of randomness
Random generation creates data that is not predictable, which is useful for testing and simulations.Step 2: Evaluate the options
Only 'It helps create unpredictable data for testing and simulations.' correctly states the importance of random generation. Options A, B, and D are incorrect because random generation does not remove the need for data cleaning, does not always produce the same output, and does not guarantee perfect accuracy.Final Answer:
It helps create unpredictable data for testing and simulations. -> Option AQuick Check:
Random generation importance = unpredictable data [OK]
Hint: Random means unpredictable data for testing [OK]
Common Mistakes:
- Thinking random data is always the same
- Assuming random data fixes all errors
- Believing random data guarantees perfect results
2. Which of the following is the correct way to generate 5 random numbers between 0 and 1 using NumPy?
easy
Solution
Step 1: Review NumPy random functions
np.random.rand(5)generates 5 random floats between 0 and 1.Step 2: Check other options
np.random.randn(5)generates samples from the standard normal distribution (not uniform [0,1));np.random.randint(0, 1, 5)returns zeros only;np.random.choice(5)picks one number from 0 to 4.Final Answer:
np.random.rand(5) -> Option BQuick Check:
Correct syntax for 5 random floats = np.random.rand(5) [OK]
Hint: Use np.random.rand(n) for n floats 0 to 1 [OK]
Common Mistakes:
- Using randint with 0 and 1 returns only zeros
- Using choice without specifying size returns one value
- Confusing randn with rand
3. What will be the output of the following code?
import numpy as np np.random.seed(0) print(np.random.rand(3))
medium
Solution
Step 1: Understand seed effect
Settingnp.random.seed(0)fixes the random numbers to a known sequence.Step 2: Check known output for seed 0
For seed 0,np.random.rand(3)produces [0.5488135 0.71518937 0.60276338]. The other options show different sequences or orders.Final Answer:
[0.37454012 0.95071431 0.73199394] -> Option CQuick Check:
Seed 0 fixed output = [0.37454012 0.95071431 0.73199394] [OK]
Hint: Seed fixes output; np.random.rand(3) gives 3 floats [OK]
Common Mistakes:
- Ignoring seed leads to different outputs
- Mixing order of numbers in output
- Confusing randint with rand
4. The following code is intended to generate 4 random integers between 1 and 10, but it raises an error. What is the problem?
import numpy as np np.random.randint(1, 10, size=4, seed=42)
medium
Solution
Step 1: Check randint parameters
NumPy'srandintdoes not accept aseedparameter directly.Step 2: Understand how to set seed
Seed must be set usingnp.random.seed(42)before callingrandint.Final Answer:
The 'seed' argument is not valid in randint function. -> Option AQuick Check:
Seed set separately, not in randint [OK]
Hint: Set seed with np.random.seed(), not in randint [OK]
Common Mistakes:
- Passing seed inside randint
- Using wrong size type
- Thinking randint is missing
5. You want to simulate rolling a fair six-sided die 1000 times using NumPy. Which code snippet correctly generates this data?
hard
Solution
Step 1: Understand die roll range
A fair six-sided die has values 1 through 6 inclusive.Step 2: Check code options
np.random.randint(1, 7, size=1000)usesrandint(1,7)which includes 1 and excludes 7, so values 1 to 6 are generated correctly.np.random.rand(1000) * 6 + 1generates floats, not integers.np.random.choice(6, size=1000)picks from default [0,1,2,3,4,5].np.random.randint(0, 6, size=1000)generates 0 to 5, which is incorrect.Final Answer:
np.random.randint(1, 7, size=1000) -> Option DQuick Check:
Correct die roll simulation = randint(1,7) [OK]
Hint: Use randint(1,7) for integers 1 to 6 [OK]
Common Mistakes:
- Using randint(0,6) gives 0 to 5
- Using rand() gives floats, not integers
- Forgetting to set size for multiple rolls
