Python Forum
fill an empty matrix with random floats
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fill an empty matrix with random floats
#1
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. ]])
Reply


Messages In This Thread
fill an empty matrix with random floats - by sofiapuvogel - Oct-29-2018, 02:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Strings to Floats samalombo 2 2,567 Jun-08-2020, 10:45 AM
Last Post: hussainmujtaba
  Matrix with bounded random numbers Felipe 2 3,785 May-21-2017, 11:31 AM
Last Post: Felipe

Forum Jump:

User Panel Messages

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