0
0
NumPydata~15 mins

np.random.rand() and random arrays in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Generating Random Arrays with np.random.rand()
📖 Scenario: Imagine you are working on a simple simulation where you need random numbers to model some data points. You will learn how to create random arrays using np.random.rand() from the NumPy library.
🎯 Goal: Build a small program that creates a random array of numbers between 0 and 1 using np.random.rand(), then select a size variable to control the array length, and finally print the generated array.
📋 What You'll Learn
Import the NumPy library as np
Create a variable called size to set the length of the random array
Use np.random.rand() with the size variable to generate the random array
Print the generated random array
💡 Why This Matters
🌍 Real World
Random arrays are useful in simulations, testing algorithms, and creating sample data for experiments.
💼 Career
Data scientists often need to generate random data to test models or simulate scenarios before working with real data.
Progress0 / 4 steps
1
Import NumPy and create a random array
Write code to import the NumPy library as np and create a variable called random_array that stores a random array of 5 numbers using np.random.rand(5).
NumPy
Need a hint?

Use import numpy as np to import NumPy. Then use np.random.rand(5) to create an array of 5 random numbers.

2
Create a size variable for the array length
Create a variable called size and set it to 7. This will control the length of the random array you will generate next.
NumPy
Need a hint?

Just write size = 7 to create the variable.

3
Generate a random array using the size variable
Use np.random.rand() with the variable size to create a new random array called random_array that has size number of elements.
NumPy
Need a hint?

Use random_array = np.random.rand(size) to create the array with the length stored in size.

4
Print the generated random array
Write a print() statement to display the contents of the random_array.
NumPy
Need a hint?

Just write print(random_array) to show the array.