kittyKat
kittyKat

Reputation: 121

How to merge values from each array to get one new array

I'm trying to combine two merge values from two arrays to get one whole new array. However, I have no idea how to do.

I want to get a random float number for two variables like 5 times because I want to store them for future use. Hence, I used math.random but it doesn't work as expected because it will replace the variables.

Hence, I tried to get a randomized number and put it into an array. Then, I want to combine them to get one array. Each random number from each array are together.

import numpy as np

np.random.seed(42)

randomReleaseAngle = np.empty(5)
randomVelocity = np.empty(5)

for i in range(5):
    randomReleaseAngle[i] = np.random.uniform(20.0, 77.0 )
    randomVelocity[i] = np.random.uniform(40.0, 60.0 )

print(randomReleaseAngle)
print(randomVelocity)

I wanted to get something like this:

[[41.34,51.72], [28.86,45.31], [54.26,44.23], [64.22,53.29], [72.27,52.13]]

Upvotes: 1

Views: 51

Answers (1)

yatu
yatu

Reputation: 88226

You can specify a size of the output array when using np.random.uniform, no need for looping:

randomReleaseAngle = np.random.uniform(20.0, 77.0, size=(5, 2))
randomVelocity = np.random.uniform(40.0, 60.0, size=(5, 2))

array([[41.34878677, 74.19071547],
       [61.72365468, 54.1235336 ],
       [28.89306251, 28.89168766],
       [23.31076589, 69.37204031],
       [54.26355567, 60.36013693]])

Upvotes: 1

Related Questions