Python Forum
fill an empty matrix with random floats - 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: fill an empty matrix with random floats (/thread-13724.html)



fill an empty matrix with random floats - sofiapuvogel - Oct-29-2018

Im new in programming and I want to fill just the first column of a matrix with diferent random float within a certain range (low and high limits), but when I run my code it fills all the rows in the first column with the same value. Please help me! :)
import numpy as np
import random
res=np.zeros([5,2])#empty matrix
first_col_res=res[:,0] #the first column of matrix res
seen=set()

def rand_teta(low, high,k):#k refers to the total random numbers:
    
    j=0
    while j <=k:
        x=random.uniform(low,high)
        
        j=j+1
        while x in seen:
            x=random.uniform(low,high)
    seen.add(x)
    first_col_res.fill(x)
    
    return res
#out put example
rand_teta(1,2,3)
Output:
Out[88]: array([[1.72762661, 0. ], [1.72762661, 0. ], [1.72762661, 0. ], [1.72762661, 0. ], [1.72762661, 0. ]])



RE: fill an empty matrix with random floats - ichabod801 - Oct-29-2018

Please use python and output tags when posting code and results. I put them in for you this time. See the BBCode link in my signature below for instructions.


RE: fill an empty matrix with random floats - ichabod801 - Oct-29-2018

I think you want res[:,0] = np.random.rand(5) + 1.