Python Forum
Matrix with bounded random numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Matrix with bounded random numbers (/thread-3390.html)



Matrix with bounded random numbers - Felipe - May-19-2017

Hi everyone, 

I want to construct a matrix with aleatory numbers that are limited by previously determined values. These values are gave in arrays which contains the upper (ub) and lower bounds (lb). I know how to create a vector/array with these characteristics, like the code below shows:

import numpy as np
from random import random

lb = np.array([1,2,3,4])
ub = np.array([5,6,7,8])

example = lb+(ub-lb)*random()
Here's an output from the code above:
Output:
[ 4.63529552  5.63529552  6.63529552  7.63529552]
Now, what's the fastest "pythonic" way to construct a matrix (4,4), which the values are limited by the upper and lower bounds? 
Here's an example of desired output using the same bounds of the code above:
Output:
[ 4.63529552  5.63529552  6.63529552  7.63529552] [ 3.53452228  4.53452228  5.53452228  6.53452228] [ 1.43902942  2.43902942  3.43902942  4.43902942] [ 2.70643708  3.70643708  4.70643708  5.70643708]
Thanks for the help.


RE: Matrix with bounded random numbers - zivoni - May-19-2017

lb + (ub - lb) * np.random.random((4, 1))
if you want for each row inequalities (lb <= row) & (row < ub)


RE: Matrix with bounded random numbers - Felipe - May-21-2017

(May-19-2017, 04:57 PM)zivoni Wrote:
lb + (ub - lb) * np.random.random((4, 1))
if you want for each row inequalities (lb <= row) & (row < ub)

Thanks !!! That solved my problem.