How to create a random array in a certain range

Clement Attlee picture Clement Attlee · Apr 4, 2016 · Viewed 8k times · Source

Suppose I want to create a list or a numpy array of 5 elements like this:

array = [i, j, k, l, m] 

where:

  • i is in range 1.5 to 12.4
  • j is in range 0 to 5
  • k is in range 4 to 16
  • l is in range 3 to 5
  • m is in range 2.4 to 8.9.

This is an example to show that some ranges include fractions. What would be an easy way to do this?

Answer

Ami Tavory picture Ami Tavory · Apr 4, 2016

You can just do (thanks user2357112!)

[np.random.uniform(1.5, 12.4), np.random.uniform(0, 5), ...]

using numpy.random.uniform.