0

I would like to generate two set of variables, h_min and h_max,

h_max = [h_max_1, h_max_2, …h_max_2000]
h_min = [h_min_1, h_min_2,… h_min_2000]

Each element of h_max is generated based on uniform distribution, i.e.,

h_max = np.random.uniform(0,  20, 2000). 

For each element, h_min_i, it should be generated based on the uniform distribution with range of 0, and corresponding h_max_i In other words, h_min_i = np.random.uniform(0, h_max_i) In stead of using iteration, how to efficiently generate h_min

user785099
  • 5,323
  • 10
  • 44
  • 62

1 Answers1

4

The numpy.random.uniform function allows the first and/or second parameters to be an array, not just a number. They work exactly as you were expecting:

h_max=np.random.uniform(0,20,2000)
h_min=np.random.uniform(0,h_max,2000)

However, numpy.random.* functions, such as numpy.random.uniform, have become legacy functions as of NumPy 1.17, and their algorithms are expected to remain as they are for backward compatibility reasons. That version didn't deprecate any numpy.random.* functions, however, so they are still available for the time being. See also this question.

In newer applications you should make use of the new system introduced in version 1.17, including numpy.random.Generator, if you have that version or later. One advantage of the new system is that the application relies less on global state. Generator also has a uniform method that works in much the same way as the legacy function numpy.random.uniform. The following example uses Generator and works in your case:

gen=np.random.default_rng() # Creates a default Generator
h_max=gen.uniform(0,20,2000)
h_min=gen.uniform(0,h_max,2000)
Peter O.
  • 32,158
  • 14
  • 82
  • 96