'Creating an Array of Uniformly Distributed RVs with changing ranges based on values in another column

I have two numpy arrays current_index which I am trying to fill, and time_to_maturity which has various times. I want to fill current_index uniformly with varying boundaries.

I see two ways to do this, do you know why both works. The first one makes sense to me:

for i in range(len(current_index)):
current_index[i] =  np.random.uniform(low=900.0*(1 - .2 *((756 - time_to_maturity[i])/252)), high=1000*(1 + .2 *((756 - time_to_maturity[i])/252)))

I know this is not the ideal way, and I want to vectorize this operation. I tried something else which seemed to work but I do not know why.

current_index =  np.random.uniform(low=900*(1 - .2 *((756 - time_to_maturity)/252)), high=1000**(1 + .2 *((756 - time_to_maturity)/252)))

Why does the second way work? I am setting the whole array to a single value with the upper and lower bounds as the whole array? I did not specify the output shape, so I am curious as to why this works.

Thank you in advanced.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source