Python Forum
Matrix with bounded random numbers
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix with bounded random numbers
#1
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.
Reply
#2
lb + (ub - lb) * np.random.random((4, 1))
if you want for each row inequalities (lb <= row) & (row < ub)
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  fill an empty matrix with random floats sofiapuvogel 2 3,267 Oct-29-2018, 12:33 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020