tylerdjt
tylerdjt

Reputation: 11

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

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.

Upvotes: 1

Views: 3319

Answers (2)

StupidWolf
StupidWolf

Reputation: 46938

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

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

Upvotes: 2

cmay
cmay

Reputation: 153

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

Upvotes: 1

Related Questions