0
0
NumPydata~10 mins

Setting random seed for reproducibility in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Setting random seed for reproducibility
📖 Scenario: Imagine you are a data scientist who needs to generate random numbers for a simulation. To make sure your results can be repeated exactly by you or others, you need to set a random seed.
🎯 Goal: You will create a numpy random number generator with a fixed seed, generate some random numbers, and print them. This ensures your random numbers are the same every time you run the code.
📋 What You'll Learn
Use numpy to generate random numbers
Set a random seed for reproducibility
Generate an array of random numbers
Print the generated random numbers
💡 Why This Matters
🌍 Real World
Setting a random seed is important in data science to ensure experiments and simulations can be repeated exactly, which helps with debugging and sharing results.
💼 Career
Data scientists and analysts often set random seeds when working with random processes to guarantee consistent results across runs and among team members.
Progress0 / 4 steps
1
Import numpy and create a random number generator
Write a line to import numpy as np. Then create a random number generator called rng using np.random.default_rng() with no seed yet.
NumPy
Need a hint?

Use import numpy as np to import numpy. Then create rng by calling np.random.default_rng() without arguments.

2
Set a random seed for reproducibility
Modify the creation of rng to use a fixed seed of 12345 by passing it as an argument to np.random.default_rng().
NumPy
Need a hint?

Pass the number 12345 inside the parentheses of np.random.default_rng() to set the seed.

3
Generate an array of random numbers
Use the rng random number generator to create an array called random_numbers of 5 random floats between 0 and 1 using rng.random(5).
NumPy
Need a hint?

Call rng.random(5) to get 5 random floats and assign to random_numbers.

4
Print the generated random numbers
Write a print statement to display the random_numbers array.
NumPy
Need a hint?

Use print(random_numbers) to show the array of random numbers.