Concept Flow - Uniform random with random()
Call np.random.random()
Return random float
Use or store the float
Repeat if needed
The function np.random.random() generates a random float between 0 and 1 each time it is called.
Jump into concepts and practice - no test required
import numpy as np x = np.random.random() print(x)
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Call np.random.random() | Generates a random float in [0,1) | 0.3745401188473625 |
| 2 | Assign to variable x | x = 0.3745401188473625 | x holds 0.3745401188473625 |
| 3 | Print x | Output the value of x | Prints 0.3745401188473625 |
| 4 | End | No more code | Execution stops |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| x | undefined | 0.3745401188473625 | 0.3745401188473625 | 0.3745401188473625 |
np.random.random() generates a float in [0,1). Each call returns a new random number. Use it to get uniform random floats. Assign to variables to store values. Useful for simulations and sampling.
What does numpy.random.random() generate by default?
numpy.random.random() generates random floats in the range [0.0, 1.0).Which of the following is the correct syntax to generate a 2x3 array of uniform random numbers using numpy.random.random()?
size parameter expects a tuple for shape, so use parentheses and commas inside.numpy.random.random((2,3)) correctly passes a tuple for size.What is the output shape of the following code?
import numpy as np arr = np.random.random(5)
Identify the error in this code snippet:
import numpy as np arr = np.random.random[3,4]
[] causes a syntax error; correct is random((3,4)).You want to create a 3x3 matrix of uniform random numbers but only want numbers greater than 0.5. Which code correctly achieves this?