Create random number array in python with numpy

Photo by olia danilevich from Pexels

Random number generation is a common operation in many areas of computer science, such as statistics, cryptography, and computer programming.

In this article, we are going to learn how to create random numbers with NumPy arrays in Python.

If you don’t have numpy you have to install numpy first

pip install numpy

1. random one-dimension integer number

np.random.randint(2, size=10)
# output
# array([1, 1, 0, 0, 1, 1, 1, 1, 0, 0])

in the first argument, we input range of random number is 2, so we got 1, 0 in a random number, the second argument, we input size of an array is 10

2. random two-dimension integer number

np.random.randint(10, size=(2, 4))
# output
# array([[6, 3, 0, 4],
# [6, 8, 2, 3]])

In the first argument, we input random range is 10, second argument we can input 1st dimension is 2 and 2nd dimension is 4.

3. random upper bound number

np.random.randint(0, [1, 10, 100])
#output
# array([ 0, 4, 61])

here we set the minimum random range for all indexes as 0, the maximum range in the 1st index is 1, the 2nd index is 10, and the 3rd index is 100.

4. random lower bound number

np.random.randint([1, 5, 9], 10)
# Output
# array([3, 7, 9])

the minimum random range n the 1st index is 1, the 2nd index is 5, and the 3rd index is 9. for all indexes as 0, the maximum range for all indexes is 10.

5. random number with type

np.random.randint([1, 5, 9], [[10], [20]], dtype=np.uint8)
# Output
# array([[ 5, 8, 9],
# [12, 10, 14]], dtype=uint8)

we also can define dimension when you select upper bound and lower bound and you can define the type whatever you what. for this case, we define [[10], [20]] it mean you want the maximum dimension-1 in index 1 to be 10 and the maximum dimension-1 in index 2 is 20.

6. random float number between zero and one

np.random.rand(2,4)
# Output
# array([[0.62998756, 0.59149609, 0.57944416, 0.485989 ],
# [0.72946946, 0.47362045, 0.14221045, 0.27405028]])

If you want to random float numbers between 0 and 1 use random(), for this case, we random dimension-1 is 2, and dimension-2 is 4.

7. multiply random int array and float array

floatArr = np.random.rand(2, 4)
intArr = np.random.randint(100, size=(2, 4))
print("floatArr: \n" + str(floatArr))
print("\nintArr: \n" + str(intArr))
multArr = np.multiply(floatArr, intArr)
print("\nmultArr: \n" + str(multArr))
# Output
# floatArr:
# [[0.33808962 0.4617744 0.73185594 0.74211359]
# [0.7618982 0.12598845 0.25036773 0.62690446]]
# intArr:
# [[43 85 7 48]
# [35 66 51 69]]
# multArr:
# [[14.53785383 39.25082407 5.12299159 35.62145242]
# [26.66643685 8.31523783 12.768754 43.25640774]]
view raw numpy-multi.py hosted with ❤ by GitHub

Sometimes you might want to random float numbers not only zero to one, you can use randint array multiply with rand.

Finally, I hope you got some ideas with numpy random. Thank you for reading and enjoy your coding 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s