Random Arrays
Problem Statement
Generate random arrays with specific properties.
Background
NumPy's random module provides:
- np.random.seed(n) - for reproducibility
- np.random.rand(m, n) - uniform [0, 1)
- np.random.randn(m, n) - standard normal
- np.random.randint(low, high, size) - integers
Your Task
Write a function random_arrays(rows, cols, seed) that creates random arrays.
Important: Set the seed at the start using np.random.seed(seed).
Output Format
Return a dictionary with:
- "uniform": Uniform random (rounded to 2 decimals)
- "normal": Normal distribution (rounded to 2 decimals)
- "integers": Random integers from 0-9
Example:
rows=2, cols=3, seed=42
Arrays with reproducible random values
np.random.seed(42) ensures reproducible results
Constraints:
- Always set seed first
- Round floats to 2 decimal places
- Integer range is 0-9 (inclusive)
More from NumPy Foundations
Background Knowledge
Random Number Generation Fundamentals
Random number generation is a cornerstone of numerical computing, used in simulations, statistical analysis, machine learning, and scientific research. NumPy provides efficient, vectorized random number generators that produce sequences of random values from different probability distributions. The key insight is that these generators are actually pseudorandom—they produce deterministic sequences that appear random but are reproducible when initialized with the same seed value. This reproducibility is critical for scientific work, debugging, and sharing results.
Seeding and Reproducibility
A seed is an initial value that determines the entire sequence of pseudorandom numbers generated afterward. By setting the same seed, you guarantee identical results across different runs—essential for debugging and validating code. Without setting a seed, each execution produces different random values. Think of the seed as the starting point of a long, predetermined sequence; different seeds lead to different sequences, but the same seed always produces the same sequence.
Distribution Types in NumPy
NumPy's random module provides generators for different probability distributions. Uniform distributions (np.random.rand) produce values evenly spread across a range (typically [0, 1)), while normal distributions (np.random.randn) produce values clustered around a mean (0) with a standard deviation of 1, following the bell curve. Integer generators (np.random.randint) produce discrete whole numbers within specified bounds. Each serves different use cases: uniform for general randomization, normal for modeling natural phenomena, and integers for discrete choices.
Algorithm/Approach
The general pattern for this problem involves three sequential steps:
- Initialize the random state by setting the seed—this ensures reproducibility
- Generate three separate random arrays using different NumPy functions, each producing a different distribution type
- Process and format the output by rounding to appropriate decimal places and organizing results into a dictionary structure
This is a straightforward application of NumPy's random module functions, where the main challenge is understanding which function produces which distribution and how to properly format the results.
Step-by-Step Strategy
Step 1: Set the Seed
- Call np.random.seed(seed) at the very beginning of your function
- This must happen before any random number generation to ensure reproducibility
Step 2: Generate Uniform Random Array
- Use np.random.rand(rows, cols) to create an array with values uniformly distributed in [0, 1)
- Round the result to 2 decimal places using NumPy's rounding function
Step 3: Generate Normal Distribution Array
- Use np.random.randn(rows, cols) to create an array with values from a standard normal distribution
- Round to 2 decimal places
Step 4: Generate Integer Array
- Use np.random.randint(low, high, size) with appropriate bounds (0 to 9 inclusive)
- Note: randint includes low but excludes high, so use high=10 for integers 0-9
- Specify size=(rows, cols) to get the correct shape
Step 5: Organize and Return
- Create a dictionary with keys "uniform", "normal", and "integers"
- Assign each processed array as the corresponding value
- Return the dictionary
Common Pitfalls
Seed Placement
- Setting the seed after generating some random numbers wastes the reproducibility benefit. Always set it first.
- Forgetting to set the seed entirely means results won't be reproducible.
Rounding Precision
- Use NumPy's np.round() function rather than Python's built-in round() for array operations—it's vectorized and more efficient.
- Ensure you round to exactly 2 decimal places as specified.
Integer Range Confusion
- Remember that np.random.randint(low, high) includes low but excludes high. To get integers 0-9, use randint(0, 10), not randint(0, 9).
- The size parameter should be a tuple (rows, cols) for 2D arrays.
Array Shape Consistency
- All three arrays should have the same shape (rows, cols). Verify this before returning.
Dictionary Key Names
- Match the exact key names specified: "uniform", "normal", and "integers" (note the plural for integers).
Time & Space Complexity
Time Complexity: O(rows×cols)
Generating each random array requires creating rows×cols values. Rounding and organizing into a dictionary are also linear operations in the number of elements. The dominant cost is the array generation itself, which scales linearly with the total number of elements.
Space Complexity: O(rows×cols)
You create three arrays, each of size rows×cols, plus the dictionary structure. The total space required is proportional to the number of elements across all three arrays, which is 3×rows×cols. This simplifies to O(rows×cols) in Big-O notation since constant factors are dropped.