Synox
Synox

Reputation: 89

Generate list of floats where each number is inferior to the number of another list of floats

I am using the following code to get a random list of floats:

random_A = np.random.uniform(low=0, high=10, size=(50,))

So I get a list of 50 floats

[0.35664866 4.76750599 2.05083389 9.53811567 5.36920383 8.91679955
 2.19965617 2.62523134 6.55224616 9.35766331 5.79652488 4.23194067
 2.72168337 5.31431884 8.3975979  9.29497168 5.42797236 5.64302212
 2.91821098 5.06305922 1.88212402 0.24593891 9.45021432 0.95423611
 9.36860165 2.46100709 3.80709829 1.08442665 3.28513088 9.75743916
 5.36187267 4.61001088 0.17912406 6.52406152 3.26927165 4.40187936
 6.79600876 8.10418648 1.06927133 5.3087785  1.85829928 2.20111922
 1.6910625  6.25714944 0.29338196 5.73195802 0.73971446 3.62506435
 9.0166149  3.90316395]

What I would like to get now is another list of 50 floats let us call it random_B where each float of that list is inferior to the number compared in the first list (random_A). Could you please help me?

Regards

Upvotes: 1

Views: 49

Answers (1)

Sheldore
Sheldore

Reputation: 39072

This is one indirect way to do it: Generate the first array and then use the individual element of that array as the upper limit for generating the second array again using np.random.uniform

import numpy as np; np.random.seed(121)

random_A = np.random.uniform(low=0, high=10, size=(10,))
print (random_A)
# [1.11330828 2.10767575 2.32962488 1.51944557 8.30178138 4.07919415 5.557906   7.45523942 2.48499756 9.68659399]

random_B = np.array([np.random.uniform(0, i) for i in random_A])
print (random_B)
# [1.06063118 1.03244254 0.04262587 1.30308582 3.36170915 2.53919206 1.64164697 6.93030501 1.00795353 5.49521109]

EDIT

Even better as suggested by @Severin in the comments below

random_B = np.random.uniform(low=0, high=random_A, size=(10,))

Upvotes: 2

Related Questions