Home > Mobile >  Generate random array with random numbers between two values, including negative ones with numpy
Generate random array with random numbers between two values, including negative ones with numpy

Time:11-07

Creating and array like

random_num = np.random.randint(10,20)
arr_rand = np.random.randint(random_num, size = (4,2))

works normally, but how do I make the minimum value a negative number? I get the two errors:

ValueError                                Traceback (most recent call last)
<ipython-input-207-7a3b85248cc6> in <module>
      1 random_num = np.random.randint(-10,20)
----> 2 arr_rand = np.random.randint(random_num, size = (4,2))

mtrand.pyx in numpy.random.mtrand.RandomState.randint()

_bounded_integers.pyx in numpy.random._bounded_integers._rand_int32()

ValueError: low >= high

Also, they should preferrably be floats. The only other way I know how to do it is with the usual randn that generates values between 0 and 1.

CodePudding user response:

I believe you are looking for np.random.uniform(-10,20). Here is the documentation.

CodePudding user response:

You don't need to call np.random.randint twice. This will work:

np.random.randint(-10,20,(4,2))
  • Related