Generating random N-dimensional arrays
For something like this, numpy is probably unbeatable in terms of performance, since its underlying functions are implemented in C.

In your case the implementation would be rather easy:

import numpy as np

def random_hyperrectangular_matrix(dimensions, min_dim_size, max_dim_size):
    shape = np.random.randint(min_dim_size, max_dim_size + 1, dimensions)
    return np.random.randint(0, 101, shape)


Note the +1 on the upper limits, since np.random.randint excludes the upper bound, whereas random.randint includes it.

Note also that dimensions is in this case limited to be less than or equal to 32 (because of limits in the internal implementation). Your algorithm will only fail once the recursion depth reaches the limit (1000 by default). But if you need more than 32 dimensions, you are probably doing something wrong, anyways...

This might also make the rest of your program (which you don't show) easier, because operations can act on each element of the array, or only certain dimensions.



With min_dim_size, max_dim_size = 2, 2 (the lowest practical numbers, and the same to eliminate one source of randomness in the timings) this consistently improves the timing (note the logarithmic scale on the y-axis):



Especially noteworthy is that at low dimensions, the numpy approach is basically constant in time. For higher dimensions it scales the same as your approach, but consistently lower.