What will the following code output?
```python
import numpy as np
arr = np.random.randint(1, 10, size=5)
print(arr)
```
A
A) A random array of integers between 1 and 9.
B
B) A random array of floats between 1 and 10.
C
C) A random array of integers between 0 and 9.
D
D) A random array of 5 strings.
Analysis & Theory
`np.random.randint(1, 10, size=5)` generates a random array of 5 integers between 1 (inclusive) and 10 (exclusive).
What does `np.random.random(size=(2, 3))` return?
```python
import numpy as np
arr = np.random.random(size=(2, 3))
print(arr)
```
A
A) A 2x3 array of random integers between 0 and 1.
B
B) A 2x3 array of random floating-point numbers between 0 and 1.
C
C) A 2x3 array of random floating-point numbers between 1 and 2.
D
D) A 2x3 array of zeros.
Analysis & Theory
`np.random.random(size=(2, 3))` returns a 2x3 array of random floating-point numbers in the range [0, 1).
How would you generate a random float between 0 and 1?
```python
import numpy as np
arr = np.random.random()
print(arr)
```
A
A) A random float between 0 and 1.
B
B) A random float between 0 and 2.
C
C) A random integer between 0 and 1.
D
D) A random float between -1 and 1.
Analysis & Theory
`np.random.random()` generates a single random float in the range [0, 1).
What does `np.random.choice([1, 2, 3, 4], size=3)` do?
```python
import numpy as np
arr = np.random.choice([1, 2, 3, 4], size=3)
print(arr)
```
A
A) It randomly selects 3 elements from the array `[1, 2, 3, 4]` and returns them.
B
B) It randomly selects 3 elements and returns a sorted array.
C
C) It randomly selects 3 elements with replacement.
D
D) It randomly selects 3 elements without replacement.
Analysis & Theory
`np.random.choice([1, 2, 3, 4], size=3)` randomly selects 3 elements with replacement from the array `[1, 2, 3, 4]`.
What does `np.random.seed(42)` do?
```python
import numpy as np
np.random.seed(42)
arr = np.random.random(size=5)
print(arr)
```
A
A) Sets the random number generator's seed for reproducibility.
B
B) Generates random numbers between 0 and 42.
C
C) Fixes the size of the random array to 42.
D
D) Initializes the random number generator with a default value.
Analysis & Theory
`np.random.seed(42)` sets the seed for the random number generator, making the random sequence reproducible.
What will be the output of the following code?
```python
import numpy as np
arr = np.random.randn(3, 2)
print(arr)
```
A
A) A 3x2 array with random values from a normal distribution (mean 0, variance 1).
B
B) A 3x2 array with random integers between 0 and 1.
C
C) A 3x2 array with random values between -1 and 1.
D
D) A 3x2 array with zeros.
Analysis & Theory
`np.random.randn(3, 2)` generates a 3x2 array of random values drawn from a normal distribution with mean 0 and variance 1.
How do you generate a random permutation of a NumPy array?
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
permuted_arr = np.random.permutation(arr)
print(permuted_arr)
```
A
A) It shuffles the array in place.
B
B) It generates a new array that is a random permutation of the original array.
C
C) It generates a random permutation without modifying the original array.
D
D) It sorts the array randomly.
Analysis & Theory
`np.random.permutation(arr)` returns a new array that is a random permutation of `arr`, leaving the original array unchanged.
What will `np.random.uniform(1, 10, size=3)` generate?
```python
import numpy as np
arr = np.random.uniform(1, 10, size=3)
print(arr)
```
A
A) 3 random floats between 1 and 10 (inclusive).
B
B) 3 random floats between 1 and 10 (exclusive).
C
C) 3 random integers between 1 and 10.
D
D) 3 random integers between 0 and 10.
Analysis & Theory
`np.random.uniform(1, 10, size=3)` generates 3 random floating-point numbers between 1 (inclusive) and 10 (exclusive).
What does `np.random.shuffle()` do to a NumPy array?
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)
```
A
A) It shuffles the elements of the array in place.
B
B) It sorts the array in ascending order.
C
C) It returns a new array with shuffled elements.
D
D) It creates a random permutation of the array and returns it.
Analysis & Theory
`np.random.shuffle()` shuffles the elements of the array in place, modifying the original array.
What will `np.random.rand(2, 3)` generate?
```python
import numpy as np
arr = np.random.rand(2, 3)
print(arr)
```
A
A) A 2x3 array with random integers between 0 and 1.
B
B) A 2x3 array with random floating-point numbers between 0 and 1.
C
C) A 2x3 array with random floats from a normal distribution.
D
D) A 2x3 array of zeros.
Analysis & Theory
`np.random.rand(2, 3)` generates a 2x3 array of random floating-point numbers between 0 and 1.